Fun with pressure pads

Not really sure where this can be used, maybe some kind of game?

The standTime is how long (in seconds) you will have to press or stand on the pads before the task is complete.

If you want to allow some short dropdowns of pressure, you can set that in the maxRest (also in seconds) and the low pressure will then be ignored for that amount of time, and the timer will then be reset if it is exceeded.

//Define the time with pressure
int days      = 0;
int hours     = 0;
int minutes   = 0;
int seconds   = 10;

//Program starting
unsigned long standTime; //Seconds to have enough pressure on both pads
unsigned long maxRest = 3; //Seconds the minimum pressure can be bypassed without resetting the timer
int minPressure = 880; //Minimum allowed pressure on both pads

//Analog
int leftPadPin = 0; //Left pressure pad
int rightPadPin = 1; //Right pressure pad

//Digital
int piezoPin = 2; //Where the piezo is connected to
int ledPin = 13; //Just for visualisation
int relayPin = 3;

//No further editing needed
long lastSit;
long lastRest;

void beep(int val)
{
  digitalWrite(ledPin, HIGH);

  for(int i=0; i < 500; i++)
  {
    digitalWrite(piezoPin, HIGH);
    delayMicroseconds(val);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(val);
  }

  digitalWrite(ledPin, LOW);
}

void setup()
{
  standTime = (seconds*1000) + ((minutes*1000)*60) + (((hours*1000)*60)*60) + ((((hours*1000)*60)*60)*24);
  maxRest = maxRest*1000;

  //Set pin modes
  pinMode(piezoPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(relayPin, OUTPUT);

  //Turn magnet on
  digitalWrite(relayPin, HIGH);

  lastSit = millis();
  lastRest = millis();
}

void loop()
{
  if (analogRead(leftPadPin) <= minPressure || analogRead(rightPadPin) <= minPressure)
  {
    if (millis() - lastRest > maxRest && lastSit != 999999999)
    {
      //Bummer, timer just go reset
      beep(100);
      lastRest = millis();
      lastSit = millis();
    }
  }
  else
  {
    lastRest = millis();

    if (millis() - lastSit > standTime && lastSit != 999999999)
    {
      lastSit = 999999999; //Yay! You made it, lets set it high so it won't keep beeping

      //Switch magnet off
      digitalWrite(relayPin, LOW);

      for(int i=0; i < 4; i++)
      {
        beep(200);
        delay(100);
      }
    }
  }
}

Leave a Reply