for my state machine, I created something which would help me in my final term project. It is very simple but i managed to take in 3 different inputs (light, temp & play) and then determine what they would output. There are 3 basic states visualized, Decreasing, Increasing and Equilibrium.
int Light = 0; //light intensity
int Temp = 0; //temperature level
int Switch = 0;//current Switch state
int Switch2 = 0;// Previous switch state
int State = 0; //Play or not Play
int PlayCount = 0; //Amount of Play
int LightCount = 0; //Amount of Light
int TempCount = 0; //Amount of Temperature
int Emotion = 0; //Current Amount of Emotion
int Emotion2 = 0;//Previous amount of Emotion
void setup() {
pinMode(2, INPUT);
pinMode(8, OUTPUT);
pinMode(9,OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
}
void loop(){
Light = analogRead(5);
Temp = analogRead(4);
Switch2 = Switch; //Assigning past value
Switch = digitalRead(2); //Assigning new value
if(Switch != Switch2 && Switch == HIGH){ //Determining & Assigning new switch state
if(State == 1){
State = 0;}
else State = 1;
}
if(State == 1){ //Accumulating Play
digitalWrite(8,HIGH);
PlayCount++;
}
else {PlayCount = PlayCount - 1; // Reducing Play
digitalWrite(8,LOW);
}
if(Light > 512) //Accumulating Light
{
LightCount++;
}else LightCount = LightCount - 1; // Reducing Light
if(Temp > 512) //Accumulating Temp
{
TempCount++;
}
else TempCount = TempCount - 1; //Reducing Temp
Emotion2 = Emotion; //Previous emotion value
Emotion = LightCount/2 + TempCount/2 + PlayCount; //Current Emotion value
if(Emotion > Emotion2){
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
Serial.println("Increasing");
}
if(Emotion < Emotion2){
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
Serial.println("Decreasing");
}
if(Emotion == Emotion2){
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
Serial.println("Equilibrium");
}
//Serial.print(LightCount/10);
//Serial.print(":");
//Serial.print(TempCount/10);
//Serial.print(":");
//Serial.println(PlayCount/10);
}
