Hello folks, very new to arduino and programming, and first time posting. I teach chemistry and physics at a small(ish) school, and had the opportunity to bring robotics and arduino to my students. I have a pretty good grasp on the electronics, but I am learning programming at the same pace (sometimes slower) than my students. You'll probably see me on here a lot XD
We are learning to use the serial monitor to provide inputs. Below is a code that is supposed to run an 8-LED array in a sequence or pattern. In theory, when the user types '1' into the serial monitor, it should start the sequence and repeat until the user types 'x'. I have tried several things (while loops, serial.peek, !=, etc), but we always have one of two problems: either the '1' input only provides on step in the sequence (to get the pattern to run, you have to enter 1 over and over) or it gets caught in the pattern loop forever and x won't clear it. Below is the code that goes through the LEDs in "steps"
/*
*/
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int power = 12; //sets power to run through Pin 12
byte patterns[30] = {
B00000011, 50,
B00000110, 50,
B00001100, 50,
B00011000, 50,
B00110000, 50,
B01100000, 50,
B11000001, 50,
B10000010, 50,
B00000100, 50,
B00001000, 50,
B00010000, 50,
B00100000, 50,
B01000000, 50,
B10000000, 50
};
int index = 0;
int count = sizeof(patterns) / 2;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(power, OUTPUT); //makes sure pin 12 provides power
Serial.begin(9600);
while (! Serial); // Wait untilSerial is ready - Leonardo
Serial.println("Enter 1 to start or 'x' to clear");
}
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
if (ch == '1')
{
digitalWrite(power,HIGH);
updateShiftRegister();
}
if (ch == 'x')
{
digitalWrite(power,LOW); //when "cleared" power to pin 12 is turned to ground
Serial.println("Cleared");
}
}
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, patterns[index * 2]);
digitalWrite(latchPin, HIGH);
delay(patterns[(index * 2) + 1]);
index++;
if (index >= count){
index = 0;
}
}
Thanks for the help and advice!