Question

In: Electrical Engineering

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

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); }


Solutions

Expert Solution

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


Related Solutions

We did a lab studying resistance and Ohm’s Law. In my lab, my light bulb circuit...
We did a lab studying resistance and Ohm’s Law. In my lab, my light bulb circuit element did not obey Ohm’s law b/c my data did not convey a proportional relationship between voltage drop and current. How do I answer: If either or both of your circuit elements did not obey Ohm’s Law, can you think of any reasons for the non-ohmic behavior? In other words what might be happening to one or both of the elements as the applied...
Hi, I am doing up my homework on mathematics and here are some questions. I want...
Hi, I am doing up my homework on mathematics and here are some questions. I want to cross-reference my answers thank you. Decide if the statements below are True or False. Please include some rough workings for me to understand: (1) Mr. Tan borrowed $500,000 from Bank XYZ at 5% annual interest to be repaid monthly over 20 years. The amount that he pays back to XYZ each month is between $3000 to $3500. (2) Continuing from (1): after 15...
What is a difference between 'Efficiency DROP' and 'Efficiency DROOP' in LEDs. Basically, I want to...
What is a difference between 'Efficiency DROP' and 'Efficiency DROOP' in LEDs. Basically, I want to know the difference between DROP and DROOP word. Please explain here in detail (don't try to use the Cambridge dictionary or Wikipedia dictionary, because I already explore that without any success) Give the SOLID answer (difference) with solid reason. Only detailed answer with solid reason will be considered as a PERFECT answer.
this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
I want my answer typed.                                      I. What i
I want my answer typed.                                      I. What is presbyopia? Hyperopia? Myopia? 2. Which cranial nerves assesses eye function and acoustic function? 3. What is the Rinne test? Weber test? 4. Do you or anyone you know have any of the issues/symptoms/conditions mentioned in the video? Is it due to genetics or aging? How is it being managed?
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
I have an unexpected indent with my python code. please find out whats wrong with my...
I have an unexpected indent with my python code. please find out whats wrong with my code and run it to show that it works here is the code : def main(): lis = inputData() customerType = convertAcct2String(lis[0]) bushCost = getBushCost(lis[0],int(lis[1],10)) flowerCost = getFlowerBedCost(int(lis[2],10),int(lis[3],10)) fertiCost = getFertilCost(int(lis[4],10)) totalCost = calculateBill(bushCost,fertiCost,flowerCost) printReciept(customerType,totalCost,bushCost,fertiCost,flowerCost) def inputData(): account, number_of_bushes,flower_bed_length,flower_bed_width,lawn_square_footage = input("Please enter values").split() return [account, number_of_bushes,flower_bed_length,flower_bed_width,lawn_square_footage] def convertAcct2String(accountType): if accountType== "P": return "Preferred" elif accountType == "R": return "Regular" elif accountType == "N": return...
What am i doing wrong. I want for the program to run through then when it...
What am i doing wrong. I want for the program to run through then when it gets to "do you want to play again?enter yes or no" if they choose yes I want it to run again if they choose no then end. def play(): print("Welcome to the Game of Life!") print("A. Banker") print("B. Carpenter") print("C. Farmer") user_input = input("What is your occupation? ").upper() if user_input == "A": money = 100 elif user_input == "B": money = 70 elif user_input...
The Table in my homework question below is completely wrong. I am not sure where I...
The Table in my homework question below is completely wrong. I am not sure where I went wrong in my calculations but coud you rework this question and answer the parts below?? Here are earnings per share for two companies by quarter from the first quarter of 2009 through the second quarter of 2012. Forecast earnings per share for the rest of 2012 and 2013. Use exponential smoothing to forecast the third period of 2012, and the time series decomposition...
hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT