In: Electrical Engineering
There is something wrong with my arduino lab. I want my four leds to light up one after the other. Then the speaker plays music, and 4 leds change with music. But my code only makes one of them work. Please help me modify my code
const int switchPin = 8; unsigned long previousTime = 0; int switchState = 0; int prevSwitchState = 0; int led = 2; // 600000 = 10 minutes in milliseconds long interval = 1000; int tonepin=9; int bpm = 120; int song[100][2] = { {AA3,Q},{AA3,Q},{AA3,Q},{F3,E+S},{C4,S},{AA3,Q},{F3,E+S},{C4,S},{AA3,H}, {E4,Q},{E4,Q},{E4,Q},{F4,E+S},{C4,S},{Ab3,Q},{F3,E+S},{C4,S},{AA3,H}, {AA4,Q},{AA3,E+S},{AA3,S},{AA4,Q},{Ab4,E+S},{G4,S}, {Gb4,S},{E4,S},{F4,E},{R,E},{Bb3,E},{Eb4,Q},{D4,E+S},{Db4,S}, {C4,S},{B3,S},{C4,E},{R,E},{F3,E},{Ab3,Q},{F3,E+S},{AA3,S}, {C4,Q},{AA3,E+S},{C4,S},{E4,H},{AA4,Q},{AA3,E+S},{AA3,S},{AA4,Q},{Ab4,E+S},{G4,S}, {Gb4,S},{E4,S},{F4,E},{R,E},{Bb3,E},{Eb4,Q},{D4,E+S},{Db4,S}, {C4,S},{B3,S},{C4,E},{R,E},{F3,E},{Ab3,Q},{F3,E+S},{C4,S}, {AA3,Q},{F3,E+S},{C4,S},{AA3,H} }; int num_notes = 70; int led_notes[] = {Ab3,AA3,F3,C4,E4,F4,Gb4,G4}; int num_leds = 8 ; int leds[] = {4,5,6,7} ; void setup() { for (int x = 4; x < 8; x++) { pinMode(x, OUTPUT); } pinMode(switchPin, INPUT); } void servo(){ myServo.attach(9); Serial.begin(9600); } void led_on_v3() { pinMode(tonepin, OUTPUT); for (int i=0; i < (num_leds-1); i++) { pinMode(leds[i], OUTPUT); } } void leds_on(int note) { for (int i=0; i < (num_leds-1); i++) { if (led_notes[i] == note) { digitalWrite(leds[i],HIGH); } } } void leds_on_v2(int note) { for (int i=0; i < (num_leds-1); i++) { if (led_notes[i] == note) { digitalWrite(leds[i],HIGH); } else { digitalWrite(leds[i],LOW); } } } void leds_off() { for (int i=0; i < (num_leds-1); i++) { digitalWrite(leds[i],LOW); } } void play_note(int note, long duration) { int blink_lights = 1; if (blink_lights == 1) { leds_on_v2(note); } if (note != R) { tone(tonepin, note, duration); delay(duration); delay(1); } } void play_song(int which_song) { for (int i=0; i < num_notes; i++) { play_note(song[i][0], song[i][1]); } } void loop() { // store the time since the Arduino started running in a variable unsigned long currentTime = millis(); // compare the current time to the previous time an LED turned on // if it is greater than your interval, run the if statement if (currentTime - previousTime > interval) { // save the current time as the last time you changed an LED previousTime = currentTime; // Turn the LED on digitalWrite(led, HIGH); // increment the led variable // in 10 minutes the next LED will light up led++; if (led == 7) { } } switchState = digitalRead(switchPin); if (switchState != prevSwitchState) { for (int x = 4; x < 8; x++) { digitalWrite(x, LOW); } led = 2; previousTime = currentTime; prevSwitchState = switchState; } prevSwitchState = switchState; play_song(0); delay(1000); }
Hi,
According to me the actual problem is coming on the void leds_on(int note) function where your for loop is not bracketed and hence it is not digital writing the pins as it runs ones and exits as the loop is not bracketed.
modified code is below and please check and revert back with queries if any.
const int switchPin = 8;
unsigned long previousTime = 0;
int switchState = 0;
int prevSwitchState = 0;
int led = 2;
// 600000 = 10 minutes in milliseconds long interval = 1000;
int tonepin=9;
int bpm = 120;
int song[100][2] = {
{AA3,Q},{AA3,Q},{AA3,Q},{F3,E+S},{C4,S},{AA3,Q},{F3,E+S},{C4,S},{AA3,H},
{E4,Q},{E4,Q},{E4,Q},{F4,E+S},{C4,S},{Ab3,Q},{F3,E+S},{C4,S},{AA3,H},
{AA4,Q},{AA3,E+S},{AA3,S},{AA4,Q},{Ab4,E+S},{G4,S},
{Gb4,S},{E4,S},{F4,E},{R,E},{Bb3,E},{Eb4,Q},{D4,E+S},{Db4,S},
{C4,S},{B3,S},{C4,E},{R,E},{F3,E},{Ab3,Q},{F3,E+S},{AA3,S},
{C4,Q},{AA3,E+S},{C4,S},{E4,H},{AA4,Q},{AA3,E+S},{AA3,S},{AA4,Q},{Ab4,E+S},{G4,S},
{Gb4,S},{E4,S},{F4,E},{R,E},{Bb3,E},{Eb4,Q},{D4,E+S},{Db4,S},
{C4,S},{B3,S},{C4,E},{R,E},{F3,E},{Ab3,Q},{F3,E+S},{C4,S},
{AA3,Q},{F3,E+S},{C4,S},{AA3,H} };
int num_notes = 70;
int led_notes[] = {Ab3,AA3,F3,C4,E4,F4,Gb4,G4};
int num_leds = 8 ;
int leds[] = {4,5,6,7} ;
void setup()
{
for (int x = 4; x < 8; x++)
{
pinMode(x, OUTPUT);
}
pinMode(switchPin, INPUT);
}
void servo()
{
myServo.attach(9);
Serial.begin(9600);
}
void led_on_v3()
{
pinMode(tonepin, OUTPUT);
//for (int i=0; i < (num_leds-1); i++)
// Modified this line where i < num_leds-1 will
initialize till pin 6 only as num_leds-1 will be 8-1=7 and i<7
will skip pin 7.
for (int i=0; i < (num_leds-1); i++)
{
pinMode(leds[i], OUTPUT);
}
}
void leds_on(int note)
{
//for (int i=0; i < (num_leds-1); i++)
// Modified this line where i < num_leds-1 will
initialize till pin 6 only as num_leds-1 will be 8-1=7 and i<7
will skip pin 7.
for (int i=0; i < (num_leds-1); i++)
{ //brakets were missing causing the
problem
if (led_notes[i] ==
note)
{
digitalWrite(leds[i],HIGH);
}
} // brackets were misssing hence looping
only once and rest leds are never written.
}
}
void leds_on_v2(int note)
{
for (int i=0; i < (num_leds-1); i++)
{
if (led_notes[i] ==
note)
{
digitalWrite(leds[i],HIGH);
}
else
{
digitalWrite(leds[i],LOW);
}
}
}
void leds_off()
{
for (int i=0; i < (num_leds-1); i++)
{
digitalWrite(leds[i],LOW);
}
}
void play_note(int note, long duration)
{
int blink_lights = 1;
if (blink_lights == 1)
{
leds_on_v2(note);
}
if (note != R)
{
tone(tonepin, note, duration);
delay(duration);
delay(1);
}
}
void play_song(int which_song)
{
for (int i=0; i < num_notes; i++)
{
play_note(song[i][0],
song[i][1]);
}
}
void loop()
{
// store the time since the Arduino started running in a
variable
unsigned long currentTime = millis();
// compare the current time to the previous time an LED turned
on
// if it is greater than your interval, run the if statement
if (currentTime - previousTime >
interval)
{
// save the current time as
the last time you changed an LED
previousTime =
currentTime;
// Turn the LED on
digitalWrite(led,
HIGH);
// increment the led
variable
// in 10 minutes the next LED
will light up
led++;
if (led == 7)
{
}
}
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState)
{
for (int x = 4; x < 8;
x++)
{
digitalWrite(x, LOW);
}
led = 2;
previousTime = currentTime;
prevSwitchState = switchState;
}
prevSwitchState = switchState;
play_song(0);
delay(1000);
}
Thanks and Regards