For a project I am using Arduino Uno to power and control two stepper motors, two buttons and an LED matrix to form a "vendin machine", I have figured out the coding to use the two buttons to power the two separate motors repsectively, check file "Code for stepper motor controled by button"..
However I was wondering how to program the LED matrix to show a certain pattern when a button is pushed. I know how to program the LED matrix to show a pattern consistently when the arduino is turned on forever, but I do not know how to program different patterns to show at different times, ie when a button is pushed or to show one pattern for 5 seconds and then another patterns 5 seconds latter. Any help will be greatly appreciated file "matrixface" shows how I have used the LED matrix before.
Code for stepper motor controled by button
int black=10;
int brown=8;
int orange=7;
int yellow=9;
int motor1=11;
int motor2=12;
int button1=1;
int button2=2;
int delayPeriod=20;
int n;
void setup()
{
pinMode(orange,OUTPUT);
pinMode(black,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(brown,OUTPUT);
pinMode(motor1,OUTPUT);
pinMode(motor2,OUTPUT);
pinMode(button1,INPUT);
pinMode(button2,INPUT);
digitalWrite(orange,LOW);
digitalWrite(black,LOW);
digitalWrite(yellow,LOW);
digitalWrite(brown,LOW);
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
}
void loop()
{
int but1 = digitalRead(button1);
if (but1 == HIGH)
{
digitalWrite(motor1,HIGH);
digitalWrite(motor2,LOW);
for (int n=0;n<6;n++)
{
digitalWrite(black,LOW);
digitalWrite(brown,HIGH);
digitalWrite(orange,HIGH);
digitalWrite(yellow,LOW);
delay(delayPeriod);
digitalWrite(black,LOW);
digitalWrite(brown,HIGH);
digitalWrite(orange,LOW);
digitalWrite(yellow,HIGH);
delay(delayPeriod);
digitalWrite(black,HIGH);
digitalWrite(brown,LOW);
digitalWrite(orange,LOW);
digitalWrite(yellow,HIGH);
delay(delayPeriod);
digitalWrite(black,HIGH);
digitalWrite(brown,LOW);
digitalWrite(orange,HIGH);
digitalWrite(yellow,LOW);
delay(delayPeriod);
}
delay(1000);
}
int but2 = digitalRead(button2);
if (but2 == HIGH)
{
digitalWrite(motor1,LOW);
digitalWrite(motor2,HIGH);
for (int n=0;n<6;n++)
{
digitalWrite(black,LOW);
digitalWrite(brown,HIGH);
digitalWrite(orange,HIGH);
digitalWrite(yellow,LOW);
delay(delayPeriod);
digitalWrite(black,LOW);
digitalWrite(brown,HIGH);
digitalWrite(orange,LOW);
digitalWrite(yellow,HIGH);
delay(delayPeriod);
digitalWrite(black,HIGH);
digitalWrite(brown,LOW);
digitalWrite(orange,LOW);
digitalWrite(yellow,HIGH);
delay(delayPeriod);
digitalWrite(black,HIGH);
digitalWrite(brown,LOW);
digitalWrite(orange,HIGH);
digitalWrite(yellow,LOW);
delay(delayPeriod);
}
delay(1000);
}
}
Code for LED matrix repeating
int R0=2;
int R1=3;
int R2=4;
int R3=5;
int R4=6;
int R5=7;
int R6=8;
int R7=9;
int CLOCK=10;
int CLOCK_INHIBT=11;
int RESET=12;
byte rowPin[]={R0,R1,R2,R3,R4,R5,R6,R7};
boolean pinState;
byte patternRow[8];
void setup()
{
pinMode(R0,OUTPUT);
pinMode(R1,OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
pinMode(R4, OUTPUT);
pinMode(R5, OUTPUT);
pinMode(R6, OUTPUT);
pinMode(R7, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(CLOCK_INHIBT, OUTPUT);
pinMode(RESET, OUTPUT);
digitalWrite(CLOCK, LOW);
digitalWrite(CLOCK_INHIBT, LOW);
digitalWrite(RESET, LOW);
patternRow[0]=B11111111;
patternRow[1]=B10000001;
patternRow[2]=B10101101;
patternRow[3]=B10010101;
patternRow[4]=B10010101;
patternRow[5]=B10101101;
patternRow[6]=B10000001;
patternRow[7]=B11111111;
}
void loop()
{
digitalWrite(RESET, HIGH);
delayMicroseconds(1000);
digitalWrite(RESET,LOW);
for (int i=0;i<8;i++)
{
digitalWrite(CLOCK,HIGH);
for (int n=0;n<8;n++)
{
if (patternRow[i] & (1<<n))
{
pinState=HIGH;
}
else
{
pinState=LOW;
}
digitalWrite(rowPin[n],pinState);
}
for (int n=0;n<8;n++)
{
digitalWrite(rowPin[n], LOW);
}
digitalWrite(CLOCK, LOW);
}
}