Clear Filters
Clear Filters

Light bulb flashes and I don't know why?

1 view (last 30 days)
Hey guys im making a small project with an arduino board that turns on and off a light based of soud and if the light is already on. But for some reason when i run the code, the lightbulb spazzes out flashing until reaching the end of the code. The code technically works, but i dont know why the light flashes like that. here is a short clip of what happens whn i run the code Shortclip. ive included the code as well. Thanks for your help!!
tic %start timer
max_samples = 100;
filter_size = 25;
threshold_value = 0.80;
close all;
tic %start timer
count=1;
while count<=max_samples
sound_data(count) = readVoltage(a,'A2');
time_data(count) = toc;
count = count+1;
end
count2=1;
while count2<=(max_samples-filter_size)
avg_sound = mean(sound_data(count2:count2+5));
Light = readVoltage(a,'A0');
if (Light<5)&&(avg_sound<threshold_value) %No light and minimal sound (keep Light off)
writeDigitalPin(a,'D2',0)
elseif (Light>=5)&&(avg_sound>threshold_value) %light on and sound (keep light on)
writeDigitalPin(a,'D2',1)
elseif (Light>=5)&&(avg_sound<threshold_value)% light on and no sound (turn light off)
writeDigitalPin(a,'D2',0)
elseif (Light<5)&&(avg_sound>threshold_value)% light off and sound on(turn light on)
writeDigitalPin(a,'D2',1)
end
count2 = count2+1;
end
  4 Comments
Walter Roberson
Walter Roberson on 8 Dec 2020
Edited: Walter Roberson on 8 Dec 2020
Reading further I see that writedigitalpin does latch. However you should probably configure the pin to pullup
Yisroel Rosenberg
Yisroel Rosenberg on 8 Dec 2020
this doesnt change anything. any other ideas

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 8 Dec 2020
Could you remind us what A0, A2, and D2 are connected to?
Your code hints that D2 is connected to the input of an LED, controlling whether it is lit or not. And it hints that A0 is the current light level. And it hints that A2 is a microphone.
If so then each time you turn off the LED, then after a sensor sampling delay of unknown length, the light sensor reading will fall. You do not have any sample buffer, so you risk entering into an unstable state, where you turn on the light because it is off, and then you immediately turn it off because it is on.
if (Light<5)&&(avg_sound<threshold_value) %No light and minimal sound (keep Light off)
writeDigitalPin(a,'D2',0)
low light, low sound, turn the light off
elseif (Light>=5)&&(avg_sound>threshold_value) %light on and sound (keep light on)
writeDigitalPin(a,'D2',1)
high light, high sound, turn the light on
elseif (Light>=5)&&(avg_sound<threshold_value)% light on and no sound (turn light off)
writeDigitalPin(a,'D2',0)
high light, low sound, turn the light off
elseif (Light<5)&&(avg_sound>threshold_value)% light off and sound on(turn light on)
writeDigitalPin(a,'D2',1)
low light, high sound, turn the light on.
So each case where the sound is high you turn the light on, and each case where the sound is low, you turn the light off. The value you read from the sensor does not have an effect on the output.
  7 Comments
Walter Roberson
Walter Roberson on 8 Dec 2020
I worked you through the cases above and showed that at present you ignore the light value. Your output currently depends only on the sound.

Sign in to comment.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!