Go Down

Topic: I was trying to toggle 8 leds with 1 button . Need help! (Read 61 times) previous topic - next topic

reincartios

May 23, 2019, 12:49 pm Last Edit: May 23, 2019, 12:53 pm by reincartios
So i was trying to control 8 leds with only one button like
press once:
B00000001
press twice:
B00000010

But the i just can't write the code can anyone help?
I'm using arduino nano.
Here's the code thats not working as intended:
int latchPin = 4;
int clockPin = 3;
int dataPin = 5;
int buttonPin = 2;
int buttonState = 0;
int buttonPressCount = 0;


byte leds = 0;
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}
void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  pinMode(buttonPin,INPUT);
}
void loop()
{
  leds = 0;
  buttonState = digitalRead(buttonPin);
  delay(500);
  if (buttonState == HIGH & buttonPressCount == 0){
    if (buttonPressCount == 0 & buttonState == HIGH)
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B00000000);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
     
    if (buttonPressCount == 1& buttonState == HIGH)
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B10000000);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
     
    if (buttonPressCount == 2 & buttonState == HIGH)
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B01000000);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
         
    if (buttonPressCount == 3 & buttonState == HIGH)
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B00100000);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
     
    if (buttonPressCount == 4 & buttonState == HIGH )
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B00010000);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
               
    if (buttonPressCount == 5 & buttonState == HIGH)
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B00001000);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
     
    if (buttonPressCount == 6 & buttonState == HIGH)
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B00000100);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
     
    if (buttonPressCount == 7 & buttonState == HIGH)
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B00000010);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
     
    if (buttonPressCount == 8 & buttonState == HIGH)
      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,B00000001);     
      digitalWrite(latchPin,HIGH);   
      delay(100);
  buttonPressCount++;
  if(buttonPressCount>8)     
    buttonPressCount=0;
  delay(300);                 
    }
    }

PaulS

Code: [Select]
  pinMode(buttonPin,INPUT);
How IS that button sewn on?

Quote
if(buttonPressCount>8)     
It makes no sense to compare buttonPressCount to a smiley face.

Code: [Select]
  buttonState = digitalRead(buttonPin);
  delay(500);
  if (buttonState == HIGH & buttonPressCount == 0){

The value in buttonState will not change while you have your head in the sand. The logical and operator is &&, not &.

You might not be able to see it, but there is a clear relationship between buttonPressCount and the value that you shift out. You have 8 times as much code as you need, with 8 times as many mistakes.
The art of getting good answers lies in asking good questions.

reincartios

the button is connected with a pull up resistor. if(buttonPressCount>8) i dont know why the 8 turned into smiley face. As im new to arduino what suggestions and tips do you have for me other than these? I really appreciate your help Thank You!

PaulS

Quote
i dont know why the 8 turned into smiley face.
Because that's what the forum software does when it sees the number 8 followed by a close parentheses.

You can prevent that by using code tags, as the stickies at the top of the forum describe.

Quote
what suggestions and tips do you have for me other than these?
The first is to always address ALL of the questions that have been asked.
The second is to use code tags when posting code.
The third is to not try to develop code all at once. Write a little bit of code. Test it. If it produces incorrect results, fix it before adding more code.
The fourth is to avoid Strings and delay()s like the plague.
The art of getting good answers lies in asking good questions.

taterking

generally with programming anytime you find yourself repeating blocks of code in your scetch there is almost always an easier way.

usually when you use a button you are not just interested in when the button is down.
you also need to know when the button has changed cause this is where you trigger your effect.
this code uses a variable to remember what the last reading was.


i am not familiar with your shift registers or shiftout, but i can tell you that if you use an array for your values it will make things much simpler for you.
then you just match the click count directly to the index number

this is untested but it's the right idea for you.

Code: [Select]

byte outs [8] {
  //put your values here seperated by commas
  };
bool laststate;
bool state;
int count;
void setup() {
  // put your setup code here, to run once:

}

void loop() {
 if(digitalRead(buttonPin)==HIGH){
  state = true;}else{state=false;}

  if(state!=oldstate){
    if(state){count++;
    if(count>7){count = 0;}

      digitalWrite(latchPin,LOW);   
      shiftOut(dataPin,clockPin,MSBFIRST,outs[count]);     
      digitalWrite(latchPin,HIGH);   
     
      }
      oldstate = state;
    }

delay(10);}

Go Up