Skip to content

Instantly share code, notes, and snippets.

@tonussi
Created October 28, 2012 03:19
Show Gist options
  • Save tonussi/3967309 to your computer and use it in GitHub Desktop.
Save tonussi/3967309 to your computer and use it in GitHub Desktop.
mealy machine implemented with arduino and output leds
/*
* creativeoffice presents
* machine state of leds
* with start button
* a fancy simulation
*/
//espaco para declaracoes de variaveis globais
//variaveis podem ser declaradas localmente tambem
//cabe ao programador decidir a logica
const int ledPin0 = 0;
const int ledPin1 = 1;
const int buttonPin = 2;
int nextStateDelay = 1000;
int selfStateDelay = nextStateDelay*2;
int state = 0;
int buttonState = 0;
void setup() {
//seta as configuraçoes/mapeamentos
pinMode(ledPin0, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
//espaco para codigo e logica em geral
//leitura do botao
buttonState = digitalRead(buttonPin);
//implementacao da maquina de estados, com dois estados apenas
if (!buttonState) {
if (state) {
digitalWrite(state, HIGH);
delay (nextStateDelay);
digitalWrite(state, LOW);
delay (nextStateDelay);
}
if (!state) {
digitalWrite(state, HIGH);
delay (nextStateDelay);
digitalWrite(state, LOW);
delay (nextStateDelay);
}
state=(state+1)%2; //states: {S0, S1}
}
else if (buttonState) {
if (state) {
digitalWrite(state, HIGH);
delay (selfStateDelay);
digitalWrite(state, LOW);
delay (selfStateDelay);
}
if (!state) {
digitalWrite(state, HIGH);
delay (selfStateDelay);
digitalWrite(state, LOW);
delay (selfStateDelay);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment