I need some help with programming Arduino with EMG signals to control the solenoid pressure valve.
I wrote some program which is setting high and low state on the output pin, but it's not working properly.
First I create a plot to observe the signal level. And the plot looks like in the picture which I attached.
The basics concept of program is setting the high signal on the output when I flex muscles and when I relax the muscle it's should write low state on the output. My idea was based on triggering the on-off signals at the moment when the muscle is flexed, the value of signal is above 600 (now the diode should be ON and stay in ON state) and it drops and when I relax muscles the value rises once again to value around 600 more or less (now the diode should be OFF) and drops under 100.
const int ledPin=13;
const int sensorPin= 0;
int level;
const int threshold1=400;
const int threshold2 =500;
void setup() {
pinMode (ledPin, OUTPUT);
pinMode (sensorPin, INPUT);
Serial.begin(9600);
}
void loop(){
level= analogRead(sensorPin);
if (level > threshold1){
digitalWrite(ledPin, HIGH);
if (level > threshold2)
digitalWrite(ledPin, LOW);
}
I wrote new code, but I still have problems with proper sending of an ON and OFF signal to the output.
When I flex the muscle I have high state on the output causing by first peak (LED is ON) but when I relax the muscle the second peak won't set the low state on the output. But when I flex the muscle once again the output change state to low and the diode is OFF. Do you have any suggestions what part of the code do I need to improve?
Here's the 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;
}
}
}
Switch(val)
then take action according to value