Go Down

Topic: LED sequence change with Button (Read 164 times) previous topic - next topic

Girishchenna

Hello

I am new to Arduino, & I have Arduino Mega 2560 board bought last month. I tried the basic examples listed in the software. I am trying to ON the 12 LEDs connected to Pins 2 - 13 with different sequences. I connected a button on pin 30. The idea is to switch ON the LEDs in different patrons, the patron selector is by the Push button. I found in the examples SWITCH Case2 & interlayed the sequences into it.
the problem i am experiencing is 1. Button input is reading or working. 2. If supply the selection through serial.read () the sequence works only one cycle. The codes are also attached.

Please help me.
Code: [Select]
[/

int inByte = 0;
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pins:
  for (int thisPin = 2; thisPin <= 13; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
  pinMode(30, INPUT);
}

void loop() {
  // read the sensor:
  if (Serial.available() > 0)
        //int inByte = Serial.read();*/
   int inByte = 0;
   int pinState = 30;
   
    for (digitalRead(pinState) == HIGH; inByte >= 3; inByte = inByte++)
    {
                Serial.println("Button ON");
                 Serial.println(inByte);
    }   
     if ( inByte >= 3 ) {
        inByte == 0;
    }
       
 switch (inByte) {
 
    case '0':{
        for ( int thisPin = 2; thisPin <= 5; thisPin++) {
        digitalWrite(thisPin, HIGH);
        delay(100);
        }
        for ( int thisPin = 10; thisPin <= 13; thisPin++) {
        digitalWrite(thisPin, HIGH);
        delay(100);
        }
        for ( int thisPin = 2; thisPin <= 5; thisPin++) {
        digitalWrite(thisPin, LOW);
        delay(100);
        }
         for ( int thisPin = 6; thisPin <= 9; thisPin++) {
        digitalWrite(thisPin, HIGH);
        delay(100);
        }
        for ( int thisPin = 10; thisPin <= 13; thisPin++) {
        digitalWrite(thisPin, LOW);
        delay(100);
        }
        for ( int thisPin = 6; thisPin <= 9; thisPin++) {
        digitalWrite(thisPin, LOW);
        delay(100);
        }
        return;
      }
       
 
    case '1':{
     
       for ( int thisPin = 2; thisPin <= 13; thisPin++){
        digitalWrite(thisPin, HIGH);
        delay(100);
       }
       for ( int thisPin = 2; thisPin <= 13; thisPin++){
        digitalWrite(thisPin, LOW);
        delay(100);
        }
        return;
    }
   
    case '2':{
     
        for ( int thisPin = 2; thisPin <= 7; thisPin++) {
        digitalWrite(thisPin, HIGH);
        delay(100);
        }
        for ( int thisPin = 2; thisPin <= 7; thisPin++) {
        digitalWrite(thisPin, LOW);
        delay(100);
        }
        for ( int thisPin = 8; thisPin <= 13; thisPin++) {
        digitalWrite(thisPin, HIGH);
        delay(100);
        }
        for ( int thisPin = 8; thisPin <= 13; thisPin++) {
        digitalWrite(thisPin, LOW);
        delay(100);
        }
    return;
    }
 
   /* case 'd':
        digitalWrite(5, HIGH);
        break;
      case 'e':
        digitalWrite(6, HIGH);
        break;*/
      default:
        // turn all the LEDs off:
        for (int thisPin = 2; thisPin < 13; thisPin++) {
          digitalWrite(thisPin, LOW);
        }
    }
  }
]

Grumpy_Mike

#1
Sep 08, 2017, 08:04 pm Last Edit: Sep 08, 2017, 08:05 pm by Grumpy_Mike
Every time you use:-
Code: [Select]
int inByte = 0;
The int part creates another variable with the same name. So reading something into a variable and then creating a new different variable with the same name is not going to work.
The button reading part then overrides what has been read from the serial part. You need to use different variables for buttons and serial, and remember that the way you have written it, when their has been a serial input it will only last for one cycle of the loop. I assume that is not what you want to do.

Girishchenna

Hai,

I tried with eliminating the serial.Read, but no success, please let me know how to switch the case / function.
If you can guide me, CALL a function & loop the same would help me lot.
All the sequences work if I eliminate the switch case function. But individual switching is not working.

The button in put is not reading. I even tried "digitalRead(pinState, INPUT_PULLUP) as found in your other post.


Grumpy_Mike

#3
Sep 10, 2017, 12:16 am Last Edit: Sep 10, 2017, 12:17 am by Grumpy_Mike
Quote
I tried with eliminating the serial.Read, but no success,
When you try something you have to post the modified code so we can see if you did it correctly. Your understanding of software is week so we have little confidence that you have understood what you were told.
It was not just the Serial.read I told you about have you changed the other things?

You also need to tell us about your wiring, a schematic would be good.

Go Up