/* TRANSMITTER*/
int potPin = A0; //pot in
int thresh = 0; // low value = heel position
int relayPin = 2; //out to relay pin line state pass on XBee
int currVal = 0; // pot values that do not get inverted or delayed or sent to serial = higher resolution
int prevVal = 0;
int potVal = 0; //pot values for serial use > XBee
int potValPrev = 0;
int potDelay = 25; // to alleviate serial buff overrun
bool setPinHigh = false;
unsigned long startTime = 0; //if (CurrentMillis - StartMillis >= IntervalMillis) is true, you’re done turn it off
unsigned long onTime = 6; // amount of time the pin is high and the relay contact is closed
unsigned long prevPotMillis = 0; // will store last time pot read
unsigned long currSloLoMillis = millis(); // for pin hi / lo
unsigned long prevSloLoMillis = 0;
int loDelay = 500;
void setup() {
Serial.begin(38400);
pinMode(relayPin, OUTPUT);
pinMode(potPin, INPUT);
}
void loop() {
unsigned long currentMillis = millis();
currVal = analogRead(potPin);
// See if the condition is true to trigger the action is true
if ((currVal > thresh && prevVal <= thresh) || (currVal <= thresh && prevVal > thresh)) {
digitalWrite(relayPin, HIGH);
setPinHigh = true;
startTime = millis();
}
// Delay for length of physical momentary connection
if (setPinHigh && millis() - startTime < onTime)
{
digitalWrite(relayPin, HIGH);
}
else // (currSloLoMillis - prevSloLoMillis >= loDelay) ??
//should this be an if statement?
{
digitalWrite(relayPin, LOW); // should always be == to pedal off in heel position
setPinHigh = false;
prevSloLoMillis = currSloLoMillis;
}
prevVal = currVal;
if (currentMillis - prevPotMillis >= potDelay) {
//Read the analog value from pot and store it to "value" variable
potVal = analogRead(potPin);
//Map the analog value to pwm value
potVal = map (potVal, 0, 1023, 255, 0);
if (abs(potVal - potValPrev) > 2)
if (potVal != potValPrev) {
Serial.print('<');
Serial.print(potVal);
Serial.println('>');
potValPrev = potVal;
Serial.flush();
//delay (25); now millis
prevPotMillis = currentMillis;
}
}
}
//delay (1);