/* TRANSMITTER*/
const byte potPin = A0; //pot in
const byte relayPin = 2; //out to relay pin line state pass on XBee
const int potDelay = 25; // to alleviate serial buff overrun
const int thresh = 0; // low value = heel position
#define setOFFdelay 1000UL
#define PinHighTime 100UL // amount of time the pin is high and the relay contact is closed
void setup() {
Serial.begin(38400);
pinMode(relayPin, OUTPUT);
pinMode(potPin, INPUT);
}
void loop() {
static byte WaWahVal = 0; //pot values for serial use > XBee
static byte WaWahPrev = 0;
static bool HeelPosition = true;
static bool PrevPosition = true;
static bool hasToBlink = false;
static unsigned long startTimeImpulse = 0; //if (millis() - startTimeImpulse >= IntervalMillis) is true, you’re done turn it off
static unsigned long setOFFdelayStarted = 0;
static unsigned long lastSentWW = 0; // will store last time pot read
if (analogRead(potPin) > thresh ) {
HeelPosition = false;
setOFFdelayStarted = 0;
if ( PrevPosition) {
hasToBlink = true;
PrevPosition = HeelPosition;
}
}
else {
HeelPosition = true;
if (!PrevPosition) {
setOFFdelayStarted = millis();
PrevPosition = HeelPosition;
}
if ((setOFFdelayStarted != 0) && ( millis() - setOFFdelayStarted >= setOFFdelay)) {
setOFFdelayStarted = 0;
hasToBlink = true;
}
}
if (hasToBlink) {
digitalWrite(relayPin, HIGH);
Serial.println("digitalWrite(relayPin, HIGH)");
startTimeImpulse = millis();
hasToBlink = false;
}
if (startTimeImpulse != 0 && (millis() - startTimeImpulse >= PinHighTime)) {
digitalWrite(relayPin, LOW);
Serial.println("digitalWrite(relayPin, LOW)");
startTimeImpulse = 0;
}
if (millis() - lastSentWW >= potDelay) {
WaWahVal = 255 - (analogRead(potPin) >> 2);
if (abs(WaWahVal - WaWahPrev) > 2) {
Serial.print('<');
Serial.print(WaWahVal);
Serial.println('>');
WaWahPrev = WaWahVal;
lastSentWW = millis();
}
}
}