You are on page 1of 2

int buttonPin = 12;

int
int
int
int

A
B
C
D

=
=
=
=

8;
9;
10;
11;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
}
void loop()
{
// Leer el estado del boton multifuncin
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
if(buttonPushCounter == 5){ buttonPushCounter = 1;}
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
switch(buttonPushCounter)
{
case 1: // Paro del motor
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
break;
case 2: // Giro a la Derecha
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
break;

case 3: // Paro del motor


digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
break;
case 4: //Giro a la Izquierda
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
break;
}
Serial.println(buttonState);
}

You might also like