Footdrop device

Hi all, I am pretty hopeless at coding but would like to learn more, unfortunately my little brain has hit a roadblock in regards to a little device I am trying to make. I would appreciate some assistance/guidance from some kind-hearted person? In short, I suffer from footdrop as result of a spinal chord injury.

I am experimenting with frequential electronic stimulation to activate the muscle to induce dorsiflexion (tibialis anterior). I'm 99% there, circuit wise, I'm just struggling with the code side of things. The project uses an off-the-shelf Tens machine (NeuroTrac Rehab) which has a remote switch, instead of using the switch, I am using a HC-SR04 ultrasonic sensor tethered to an Attiny85 & a home-made transistor circuit to replace the remote switch from the Tens machine. I don't intend to hack the Tens machine because of the risks involved, hence why I'm just replacing the switch.

My major problem is that the Tens unit needs a HIGH signal to "turn on" then another HIGH signal to turn off, so basically, I'm trying to get the ultrasonic sensor to read a distance & turn the Tens on when my foot is a certain distance from the ground & then turn off again when my foot is back on the ground, in this case I've made it 10cm but I would also ideally like to set another parameter i.e. if more than 50cm do nothing (LOW).

The code below is where I am stuck, I hope this is not too confusing.

#define trigPin 2

#define echoPin 3

#define tens 1

void setup()

{ Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(tens, OUTPUT);

}

void loop()

{ 

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance > 10)

{ digitalWrite(tens,HIGH);
delay(1000);
digitalWrite(tens,LOW);

}

else {

digitalWrite(tens,LOW);

}

Serial.print(distance);

Serial.println(" cm");

delay(500);
}

Thanks in advance.

If I understood what you want correctly, this might help.

#define trigPin 2
#define echoPin 3
#define tens 1

boolean tens_on = false;      // tracks tens on or off... start with LOW

void setup()
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(tens, OUTPUT);
  randomSeed(analogRead(5));
}

void loop()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  digitalWrite(tens, LOW);                  // start with this LOW
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  distance =  random(80);  // this is just some test data, comment it out to run real data

  if (distance <= 50)  // do nothing unless < 50
  {
    if (tens_on == true) // tens is on so check for off
    {
      if (distance < 10)
      {
        change();
        tens_on = false; // show it as off
      }
    }
    else  // it is off so check for on
    {
      if (distance >= 10)  // 10-50 turn it on... 
      {
        change();
        tens_on = true; // show it as on
      }
    }
  }

  Serial.print(distance);
  Serial.print(" cm ");
  if (distance <= 50)
  {
    if (tens_on)
      Serial.println("ON");
    else
      Serial.println("OFF");
    delay(500);
  }
  else
    Serial.println("Nothing");
}

void change()
{
  digitalWrite(tens, HIGH);
  delay(1000);
  digitalWrite(tens, LOW);
}

I should be semi-sober tomorrow to have another dig. Maybe. No promises. That code looped, but I think I might have learnt a couple of thingd. Declaring the bool & yeh, I suck at code & commenting. Sorry. But thanks for your efforts Picky, can I buy you a drink for your trouble or donate to arduino.cc? (Long term, I'm hoping to learn enough to contribute back)

Ant_OC: I suck at code & commenting. Sorry.

Your future self will thank you profusely if you overcome this. Descriptive variable names go a long way toward that end.

dougp: Your future self will thank you profusely if you overcome this. Descriptive variable names go a long way toward that end.

Thanks mate, will keep this in mind.