Store Light Readings in an Array

Get voltage readings from a light dependent resistor (LDR) light sensor and store in an array called ldr. Next we plot the readings stored in the array. The store_data task collects the readings and stores them the plot_data task plots them.
The LDR is attached to +3v and a 100K resistor is below it.

Program


---

Get voltage readings from a light dependent resistor (LDR)

light sensor and store in an array called ldr.

Next we plot the readings stored in the array.

The store_data task collects the readings and stores them

the plot_data task plots them.

---

prog_vars =

d/ldr[1000] 0

prog_vars.


prog_start =

print "Plot ldr data"

-- Setup the LDR sensor on Port B1

-- Range is 0 to 100

gvr b1 0 100

-- Setup USER led

pwm d1 0

prog_start.


prog_loop =

store_data 50

plot_data 50

exitnow

prog_loop.



store_data /no_of_points 0 =

-- Get light readings from the ldr and store

-- in the /ldr array


print "Get /no_of_points of light readings"

d/point_no 0

-- Get the ldr readings and store

repeat /no_of_points ->

-- Store the new value.

/ldr[/point_no] /b1

print "/point_no /b1"

-- Plot the sine value we just stored

-- tbar . /b1 0 25 "/point_no /b1"

-- Set USER brightness based on plot value

pwm d1 /b1

inc /point_no

repeat.

store_data.



plot_data /plot_points 0 =

-- Plot the ldr stored in the /ldr array.


print "Plot /plot_points Stored in LDR data Array"

d/plot_no 0

pwm d1 0

repeat /plot_points ->

-- Plot the ldr data

tbar . /ldr[/plot_no] 0 25 "B /plot_no /ldr[/plot_no]"

-- print "B /plot_no /ldr[/plot_no]"

-- Set USER brightness based on plot value

pwm d1 /ldr[/plot_no]

inc /plot_no

repeat.

plot_data.



prog_stop =

print "Good bye"

prog_stop.