High and low state With Emg control

Hey guys with some help i wrote a need code but still the effect is not so good .

My idea is base on triggering the on- off signals in moment when the muscle is flex the value of signal is above 600 (now the diode should be ON and stay in ON state) and it`s drops and when i relax muscles the value rise once again to value around 600 more or less (now the diode should be OFF ) and drops under 100 . In attachment i add the plot with signal values .

When i flex muscle i have high state on the output causing by first peak ( LED is ON) but when i relax muscle the second peak don't set the low state on the output . But when i felx muscle once again the output change state to low and the diode is OFF. Dou you have any sugesstion what part of code i need to improve .

Thanks for help !

Here's a code :

const int ledPin=13; //
const int sensorPin= 0; //

int level;
int flex= 0;
int PreviousState = 501;//
const int threshold1=600;//

void setup() {
pinMode (ledPin, OUTPUT); //
pinMode (sensorPin, INPUT); //
Serial.begin(9600); //
}

void loop(){
level= analogRead(sensorPin); //
if (level > threshold1 && PreviousState < threshold1){

{
flex =!flex;
}

if (flex == 1)
{

digitalWrite(ledPin, HIGH); //
}
else
{
digitalWrite(ledPin, LOW);

}

PreviousState = level;

}

}

So all it needs to do is turn the LED on when it is above the threshold and turn it off when it is below the threshold? The current code seems unnecessarily complex.

Try replacing the loop with this

void loop()
{
  level = analogRead(sensorPin); //
  if (level > threshold1)
  {
    digitalWrite(ledPin, HIGH); //
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }

  delay(2);
}

Someone with seven posts should already know this...

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Following these instructions may make it more obvious what the logic problem is. The compiler does not care if you throw blank lines and curly brackets at it, but as a human these things may affect the way that you look at your logic. If this is not enough, then some Serial.print(...) statements that tell you where you are and what the value of your variables are may help you out.

Some clearer specifications may help as well.

Good Luck!

Thanks for reply saximus

My first code looks like the code you send me :slight_smile: But i need to turn ON the diode when the level is above threshold and the diode should be ON even when the signal drops down .Because the muscle after flex tends to be tired and the value of signal drops very fast . And the diode should turn off when i relax my muscle then the signal again is above threshold and the diode should turn OFF .