필터 지우기
필터 지우기

Light bulb flashes and I don't know why?

조회 수: 1 (최근 30일)
Yisroel Rosenberg
Yisroel Rosenberg 2020년 12월 7일
댓글: Walter Roberson 2020년 12월 8일
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
Walter Roberson
Walter Roberson 2020년 12월 8일
편집: Walter Roberson 2020년 12월 8일
Reading further I see that writedigitalpin does latch. However you should probably configure the pin to pullup
Yisroel Rosenberg
Yisroel Rosenberg 2020년 12월 8일
this doesnt change anything. any other ideas

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 12월 8일
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
Yisroel Rosenberg
Yisroel Rosenberg 2020년 12월 8일
yes i do thats whats in the code
Walter Roberson
Walter Roberson 2020년 12월 8일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by