arduino state machine code examples - Making Things Interactive
arduino state machine code examples - Making Things Interactive
arduino state machine code examples - Making Things Interactive
- No tags were found...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
currentState has an unrecognized value! Danger! // print an error Serial.print("BAD STATE: "); Serial.println(currentState); // Now, what should we do? go back to off? same? My personal // preference is to go back to the start <strong>state</strong>, in this case, off. // // if we were using a momentary switch to start things, we'd have // to push the momentary switch again to start the sensor. If // we're using an on/off switch, the <strong>state</strong> <strong>machine</strong> will start up // again if the switch is still in the on position. currentState = sOff;}}void SetState(){// read our photosensor and see if it's darker, lighter,// or the same, then set the <strong>state</strong> accordingly.// we use a "noise" factor when making our calculation so that minor// fluctuations in the signal do not cause the <strong>state</strong> <strong>machine</strong> to change// <strong>state</strong>.int val = analogRead(photoPin);Serial.print(currentValue);Serial.print(" ");Serial.println(val);if ((val -noise) > currentValue) {currentValue = val;currentState = sBrighter;}else if ((val + noise) < currentValue) {currentValue = val;currentState = sDarker;}else {currentState = sSame;}}void LightSameLed(){