There is something wrong with my arduino lab. I want my four leds to light up...

70.2K

Verified Solution

Question

Electrical Engineering

There is something wrong with my arduino lab. I want my four ledsto light up one after the other. Then the speaker plays music, and4 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; intswitchState = 0; int prevSwitchState = 0; int led = 2; // 600000 =10 minutes in milliseconds long interval = 1000; int tonepin=9; intbpm = 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; intled_notes[] = {Ab3,AA3,F3,C4,E4,F4,Gb4,G4}; int num_leds = 8 ; intleds[] = {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 (inti=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, longduration) { 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 Arduinostarted 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 currenttime 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 lightup 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); }


Answer & Explanation Solved by verified expert
4.1 Ratings (634 Votes)
Hi According to me theactual problem is coming on the void ledsonint notefunction 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    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students