Arduino demo: State machine with push buttons and LEDs
2.4 هزار بار بازدید -
9 سال پیش
-
This is a short presentation
This is a short presentation of a demo where I implement a simple state machine using an Arduino, a push button and three LEDs.
The state machine goes to he next state whenever a push button is pressed. The trigger can be anything, such as a specific reading from a sensor and the state can perform any action desired.
To keep this demo simple I just used a push button to cycle through 3 LEDs where the first state turns them all off and each progressive state turns them on, one by one.
Sketch:
/*
Connect the positive side of your LED to Arduino digital pin 13
Connect the negative side of your LED to a 330 Ohm resistor
Connect the other side of the resistor to ground
*/
// First we'll set up constants for the pin numbers.
// This will make it easier to follow the code below.
const int button1Pin = 2; // pushbutton 1 pin
const int led1Pin = 13; // LED 1 pin
const int led2Pin = 12; // LED 2 pin
const int led3Pin = 11; // LED 3 pin
int iState;
bool bWasPressed; //Variable to see if the buton was pressed
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
// Set up the LED pin to be an output:
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
//Default to state 0
iState = 0;
bWasPressed = false;
// initialize serial communication at 9600 bits per second:
// Serial.begin(9600);
}
void loop()
{
int iBtnState; // variable to hold the pushbutton state
// iBtnState: HIGH = button not pressed
// LOW = button is pushed
iBtnState = digitalRead(button1Pin);
//Now determine whether I need to change state.
if (iBtnState == 1)
{
//The button is not pressed. If the button was just
//released then go on ahead to the next state.
if (bWasPressed==1)
{
//Yes, the button was just released.
//Move to the next state.
iState++;
iState = iState % 4;
//Reset the button pressed flag.
bWasPressed = false;
}
}
else
{
//The button is pressed, just set the flag.
bWasPressed = true;
}
//Take action depending on the state of the machine.
switch (iState)
{
case 0:
//Turn off all LEDs
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
break;
case 1:
//Turn on first LED
digitalWrite(led1Pin, HIGH);
break;
case 2:
//Turn on second LED
digitalWrite(led2Pin, HIGH);
break;
case 3:
//Turn on third LED
digitalWrite(led3Pin, HIGH);
break;
}
}
The state machine goes to he next state whenever a push button is pressed. The trigger can be anything, such as a specific reading from a sensor and the state can perform any action desired.
To keep this demo simple I just used a push button to cycle through 3 LEDs where the first state turns them all off and each progressive state turns them on, one by one.
Sketch:
/*
Connect the positive side of your LED to Arduino digital pin 13
Connect the negative side of your LED to a 330 Ohm resistor
Connect the other side of the resistor to ground
*/
// First we'll set up constants for the pin numbers.
// This will make it easier to follow the code below.
const int button1Pin = 2; // pushbutton 1 pin
const int led1Pin = 13; // LED 1 pin
const int led2Pin = 12; // LED 2 pin
const int led3Pin = 11; // LED 3 pin
int iState;
bool bWasPressed; //Variable to see if the buton was pressed
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
// Set up the LED pin to be an output:
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
//Default to state 0
iState = 0;
bWasPressed = false;
// initialize serial communication at 9600 bits per second:
// Serial.begin(9600);
}
void loop()
{
int iBtnState; // variable to hold the pushbutton state
// iBtnState: HIGH = button not pressed
// LOW = button is pushed
iBtnState = digitalRead(button1Pin);
//Now determine whether I need to change state.
if (iBtnState == 1)
{
//The button is not pressed. If the button was just
//released then go on ahead to the next state.
if (bWasPressed==1)
{
//Yes, the button was just released.
//Move to the next state.
iState++;
iState = iState % 4;
//Reset the button pressed flag.
bWasPressed = false;
}
}
else
{
//The button is pressed, just set the flag.
bWasPressed = true;
}
//Take action depending on the state of the machine.
switch (iState)
{
case 0:
//Turn off all LEDs
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
break;
case 1:
//Turn on first LED
digitalWrite(led1Pin, HIGH);
break;
case 2:
//Turn on second LED
digitalWrite(led2Pin, HIGH);
break;
case 3:
//Turn on third LED
digitalWrite(led3Pin, HIGH);
break;
}
}
9 سال پیش
در تاریخ 1394/08/03 منتشر شده
است.
2,478
بـار بازدید شده