7

(avery) #1
FORGE

optimised. The problem we are trying to solve now is
when to turn on an LED, based on ambient light – i.e.
a night light system. So we are looking for the optimal
threshold of when to turn on the light (see pseudo
code below):


IF light sensor reading < optmised threshold {
turn on led
} ELSE {
turn off led
}

Just optimising a single value is a bit boring, so we’ll
make it a little more complex. In the classic ‘night
light’, the light is programmed to come on when it
gets dark and vice versa. However, we also want our
system to automatically learn if the user wants the light
to turn on if the ambient light is bright and vice versa
(see pseudo code below).


IF light sensor reading < optmised threshold {
IF optimised flip behaviour flag == TRUE{
turn on led
} ELSE{
turn off led
}
} ELSE {
IF optimised flip behaviour flag == TRUE{
turn off led
} ELSE{
turn on led
}
}

So, we have two variables in our program that we
want to optimise: a sensor threshold variable (stored
as an int), and a behaviour flip flag variable (stored as
a Boolean). So the Arduino code for our ‘model’ that
predicts if the LED should be on or off based on these
variables is as follows:


bool    predict(int sensor_val, int thresh, bool    flip_
behaviour_flag){
if (flip_behaviour_flag){
return (sensor_val < thresh);
}
else{
return (sensor_val > thresh);
}
}

TRAINING
Because we are only trying to find the optimal values
for a single integer between 1 and 1024 (range of light
sensor readings), and a single Boolean which is either


YOU’LL NEED
Arduino
Light-dependent
resistor (LDR)
LED
Push-button
(push-to-make)
2 × 100 kΩ
resistors
1 kΩ resistor

TRUE or FALSE, we can simply test every combination
of values to see which one performs best, but how do
we know if a combination of values is good or not?
We score the performance of a combination of
variable values by using collected user data. In our
case, whenever the user toggles the LED manually,
our system will store the current ambient light sensor
value, along with the state that the LED is toggling
to. For example, if the LED is currently ‘off’ and the
ambient light sensor is outputting a value of 700, and
then the user tries to toggle the LED: because the user
wants the LED to be ‘on’ when the ambient sensor is
outputting 700, we store the value of 700 alongside
the value of ‘on’ in an array of user data. Below is an
example of some collected user data:

LED | AMBIENT LIGHT
——————————
Off – 100
Off – 300
Off – 600
On – 800
On – 850

Hopefully you can see that the user data above
indicates that the user likes the LED to be on when the
ambient light sensor is greater than 600.
Now that we have some user data, we can go back
to the task of judging whether or not a combination of
variable values for our prediction model is good or

Right
A breadboard is an
easy way of prototyping
everything, but you
might want to consider a
protoboard if you decide to
make your intelligent
light permanent
Free download pdf