Skip to main content
Format code, fix typos
Source Link
per1234
  • 4.3k
  • 2
  • 24
  • 44

Arduino with 8 ledLEDs and a switch

Hi iI have an assignment where inwherein we have 8 ledsLEDs connected to an arduinoArduino Uno which havehas a switch , these led. These LEDs light up according to the johnson counter but everytime ievery time I turn the switch off it should reset back to the start an. An example is if iI turned the switch off while the pin is at 8 going back to 1 , then when i turn it on it should be back to 1 again. iI got the 1 to 8 reset part right but the 8 to 1, I'm having trouble solving. Here

Here is my code , I used an arduino uno:

   int pin;
int sw3 = 10;
void setup() {
  // put your setup code here, to run once:
  for(pin = 2; pin<=9; pin++){
  
    pinMode(pin, OUTPUT);
  }
    pinMode(sw3,INPUT);
  
}
void loop() {
  pin==2;
    if(digitalRead(sw3)==HIGH){
    for(pin = 2; pin<= 9; pin++)
    {
    digitalWrite(pin, HIGH);
    delay(200);
    }
   }
   else
    {
    digitalWrite(pin,LOW);
    }     
     
   delay(200);
    
     for(pin = 2; pin<=9; pin++)
    {
    digitalWrite(pin, LOW);
    }
    delay(200);
 
  if(digitalRead(sw3)==HIGH){
    for(pin = 9; pin>= 2; pin--)
    {
    digitalWrite(pin, HIGH);
    delay(200);
    }
}
        else
     {
      
     digitalWrite(pin,LOW);
     }  
 
    delay(200);
   
    for(pin = 9; pin>=2; pin--)
    {
      digitalWrite(pin, LOW);
    }
      delay(200);   
     {
     if(digitalRead(sw3)==LOW)
     {
      digitalWrite(pin,LOW);
     }
     else
     {
      digitalWrite(pin,LOW);
     }
}
}
int pin;
int sw3 = 10;

void setup() {
  // put your setup code here, to run once:
  for (pin = 2; pin <= 9; pin++) {
    pinMode(pin, OUTPUT);
  }
  pinMode(sw3, INPUT);
}

void loop() {
  pin == 2;
  if (digitalRead(sw3) == HIGH) {
    for (pin = 2; pin <= 9; pin++) {
      digitalWrite(pin, HIGH);
      delay(200);
    }
  }
  else {
    digitalWrite(pin, LOW);
  }

  delay(200);

  for (pin = 2; pin <= 9; pin++)
  {
    digitalWrite(pin, LOW);
  }
  delay(200);

  if (digitalRead(sw3) == HIGH) {
    for (pin = 9; pin >= 2; pin--) {
      digitalWrite(pin, HIGH);
      delay(200);
    }
  }
  else {

    digitalWrite(pin, LOW);
  }

  delay(200);

  for (pin = 9; pin >= 2; pin--) {
    digitalWrite(pin, LOW);
  }
  delay(200);
  {
    if (digitalRead(sw3) == LOW) {
      digitalWrite(pin, LOW);
    }
    else {
      digitalWrite(pin, LOW);
    }
  }
}

Arduino with 8 led and a switch

Hi i have an assignment where in we have 8 leds connected to an arduino which have a switch , these led light up according to the johnson counter but everytime i turn the switch off it should reset back to the start an example is if i turned the switch off while the pin is at 8 going back to 1 , then when i turn it on it should be back to 1 again. i got the 1 to 8 reset part right but the 8 to 1, I'm having trouble solving. Here is my code , I used an arduino uno

   int pin;
int sw3 = 10;
void setup() {
  // put your setup code here, to run once:
  for(pin = 2; pin<=9; pin++){
  
    pinMode(pin, OUTPUT);
  }
    pinMode(sw3,INPUT);
  
}
void loop() {
  pin==2;
    if(digitalRead(sw3)==HIGH){
    for(pin = 2; pin<= 9; pin++)
    {
    digitalWrite(pin, HIGH);
    delay(200);
    }
   }
   else
    {
    digitalWrite(pin,LOW);
    }     
     
   delay(200);
    
     for(pin = 2; pin<=9; pin++)
    {
    digitalWrite(pin, LOW);
    }
    delay(200);
 
  if(digitalRead(sw3)==HIGH){
    for(pin = 9; pin>= 2; pin--)
    {
    digitalWrite(pin, HIGH);
    delay(200);
    }
}
        else
     {
      
     digitalWrite(pin,LOW);
     }  
 
    delay(200);
   
    for(pin = 9; pin>=2; pin--)
    {
      digitalWrite(pin, LOW);
    }
      delay(200);   
     {
     if(digitalRead(sw3)==LOW)
     {
      digitalWrite(pin,LOW);
     }
     else
     {
      digitalWrite(pin,LOW);
     }
}
}

Arduino with 8 LEDs and a switch

I have an assignment wherein we have 8 LEDs connected to an Arduino Uno which has a switch. These LEDs light up according to the johnson counter but every time I turn the switch off it should reset back to the start. An example is if I turned the switch off while the pin is at 8 going back to 1 , then when i turn it on it should be back to 1 again. I got the 1 to 8 reset part right but the 8 to 1, I'm having trouble solving.

Here is my code:

int pin;
int sw3 = 10;

void setup() {
  // put your setup code here, to run once:
  for (pin = 2; pin <= 9; pin++) {
    pinMode(pin, OUTPUT);
  }
  pinMode(sw3, INPUT);
}

void loop() {
  pin == 2;
  if (digitalRead(sw3) == HIGH) {
    for (pin = 2; pin <= 9; pin++) {
      digitalWrite(pin, HIGH);
      delay(200);
    }
  }
  else {
    digitalWrite(pin, LOW);
  }

  delay(200);

  for (pin = 2; pin <= 9; pin++)
  {
    digitalWrite(pin, LOW);
  }
  delay(200);

  if (digitalRead(sw3) == HIGH) {
    for (pin = 9; pin >= 2; pin--) {
      digitalWrite(pin, HIGH);
      delay(200);
    }
  }
  else {

    digitalWrite(pin, LOW);
  }

  delay(200);

  for (pin = 9; pin >= 2; pin--) {
    digitalWrite(pin, LOW);
  }
  delay(200);
  {
    if (digitalRead(sw3) == LOW) {
      digitalWrite(pin, LOW);
    }
    else {
      digitalWrite(pin, LOW);
    }
  }
}
Source Link

Arduino with 8 led and a switch

Hi i have an assignment where in we have 8 leds connected to an arduino which have a switch , these led light up according to the johnson counter but everytime i turn the switch off it should reset back to the start an example is if i turned the switch off while the pin is at 8 going back to 1 , then when i turn it on it should be back to 1 again. i got the 1 to 8 reset part right but the 8 to 1, I'm having trouble solving. Here is my code , I used an arduino uno

   int pin;
int sw3 = 10;
void setup() {
  // put your setup code here, to run once:
  for(pin = 2; pin<=9; pin++){
  
    pinMode(pin, OUTPUT);
  }
    pinMode(sw3,INPUT);
  
}
void loop() {
  pin==2;
    if(digitalRead(sw3)==HIGH){
    for(pin = 2; pin<= 9; pin++)
    {
    digitalWrite(pin, HIGH);
    delay(200);
    }
   }
   else
    {
    digitalWrite(pin,LOW);
    }     
     
   delay(200);
    
     for(pin = 2; pin<=9; pin++)
    {
    digitalWrite(pin, LOW);
    }
    delay(200);
 
  if(digitalRead(sw3)==HIGH){
    for(pin = 9; pin>= 2; pin--)
    {
    digitalWrite(pin, HIGH);
    delay(200);
    }
}
        else
     {
      
     digitalWrite(pin,LOW);
     }  
 
    delay(200);
   
    for(pin = 9; pin>=2; pin--)
    {
      digitalWrite(pin, LOW);
    }
      delay(200);   
     {
     if(digitalRead(sw3)==LOW)
     {
      digitalWrite(pin,LOW);
     }
     else
     {
      digitalWrite(pin,LOW);
     }
}
}