Hello, I am new to Arduino and while trying out a tutorial that came with a kit, I had an idea that I could make different sequences light up by pressing a button. There are no errors, but it isn't exactly doing what I wanted. It isn't controlled by the button at all and instead just does the first sequence(supposed to be activated by pressing the button once) repeatedly. The code is below.
int latchPin = 4;//Pin 12 (RCLK) of the shift register to pin 4 on the Arduino – this will be referred to as the “latch pin”
int clockPin = 3;//Pin 11 (SRCLK) of the shift register to pin 5 on the Arduino – this will be referred to as the “clock pin”
int dataPin = 2;//Pin 14 (SER) of the shift register to pin 2 on the Arduino – this will be referred to as the “data pin”
int buttonPin = 7;
byte leds = 0;//a variable called 'leds' is defined. This will be used to hold the pattern of which LEDs are currently turned on or off. Data of type 'byte' represents numbers using eight bits. Each bit can be either on or off, so this is perfect for keeping track of which of our eight LEDs are on or off.
byte leds2 = 7;
int currentLED = 0;
int currentLED2 = 0;
int buttonToggle = 0;
void setup()
{
//The 'setup' function just sets the three pins we are using to be digital outputs.
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
leds = 0;
leds2 = 7;
buttonToggle = 0;
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
buttonToggle++;
}
else if (buttonToggle = 5) {
buttonToggle = 0;
}
if (buttonToggle = 1) {
leds = 0;
if (currentLED == 7) {
currentLED = 0;
}
else {
currentLED++;
}
bitSet(leds, currentLED);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
delay(250);
}
else if (buttonToggle = 2) {
leds = 0;
if (currentLED == 7) {
currentLED = 0;
}
else {
currentLED++;
}
bitSet(leds, currentLED);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
if (currentLED2 == 0) {
currentLED2 = 7;
}
else {
currentLED2 = -currentLED + 7;
}
bitSet(leds, currentLED2);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
delay(250);
}
else if (buttonToggle = 3) {
if (leds == 7) {
leds = 0;
}
else {
leds++;
}
if (currentLED == 6) {
currentLED = 0;
}
else {
currentLED++;
}
bitSet(leds, currentLED);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
delay(150);
}
else if (buttonToggle = 4) {
digitalWrite(latchPin, LOW);
}
delay(100);
}