/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
const int LedRelay = 12;
//Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
enum state {
FirstRF, SecondRF, Heat, OFF // Rider fall. Is needed to know if tilted for more than 2 seconds, to discard casual shaking(tilting) and avoid strobing whenever not needed.
};
state current_state;
unsigned long previousMillis = 0; //will store last time
const long period = 10000;
unsigned long previousMillisB = 0; //will store last time
const long periodB = 20000;
void setup() {
Serial.begin(9600);
pinMode( LedRelay,OUTPUT);
digitalWrite(LedRelay, HIGH);
delay (1000);
//digitalWrite(Relay1, LOW);
digitalWrite(LedRelay, LOW);
//current_state = OFF;
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
Serial.print (current_state);
Serial.print (" ");
Serial.print (analogRead(potpin)) ;
Serial.print (" ");
Serial.print (previousMillis) ;
Serial.print (" ");
Serial.print (millis()) ;
Serial.print (" ");
Serial.println (previousMillisB) ;
// waits for the servo to get there
delay(100);
switch (current_state) { // The next logic statements are necessary to discern if biker is really on the ground or is a casual shaking. If position on the ground
case FirstRF: //0
digitalWrite(LedRelay, LOW); //NEW
if ( (val > (300)) && (val < (500)) ) {
previousMillis = millis();
current_state = SecondRF;
}
break;
case SecondRF: //1
if ( (millis() - previousMillis < period )) {
if ( (val > (600)) && (val < (800))) {
previousMillisB = millis();
current_state = Heat;
}
}
else {
current_state = FirstRF;;
}
break;
case Heat: //2
if ( (millis() - previousMillisB < periodB ) ) {
digitalWrite(LedRelay, HIGH);
previousMillis = millis();
current_state = SecondRF;
} else {
previousMillisB = millis(); //new
current_state = Heat; //new
}
break;
case OFF: //3
if ( ( (val > (600)) && (val < (800))) && (millis() - previousMillis < period )) {
previousMillisB = millis();
current_state = Heat;
} else {
digitalWrite(LedRelay, LOW);
current_state = FirstRF;
break;
}
}
}