I need some help with programming arduino with emg signals to control the solenoid pressure valve.
I wrote some program which seting 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 in output when i flex muscles and when i relax the muscle it`s should write low state on the output. Here is my code but i don't know what i must change to have proper functionality of my code . Thank you for your help ???
const int ledPin=13; //
const int sensorPin= 0; //
int level; //
const int threshold1=400;//
const int threshold2 =500;
I think you just need a little bit of syntax help:
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);
}
}
Maybe add Serial.println(level); also to confirm that level is as expected.
Right now 0 to 399 is off until 400 is reached the first time, then 400 to 499 is on, and only 500 and to 1023 is off.
So if you reach say 425 and then drop to 250, the LED will turn on and stay on, until 500+ is reached. Is that the intent?
Maybe i'm thinking in wrong way or the way which i wanted to do this is worng. But my idea was based 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 . The values of threshold which i use in code is not perfectly matched because i change it many times .
And answering to you question that's not my intention to use those values. Did you saw my emg signal plot which i attached ? Maybe if you have some idea can you improve my code . Many Thanks.
I see, so you turn the LED on as the values are rising, and then turn it off as the values are falling.
What I might suggest is comparing two readings:
void loop(){
val1 = analogRead(A0);
val1 = analogRead (A0); // gives ADC a chance to settle if source does not have much current drive
val2 = analogRead(A0);
val2 = analogRead(A0); // also enforces some time between readings, ~ 100-110uS
if ((slope == 0) && (val2 > threshold1) && (val2 > val1)) { // check for values after a prior falling edge, now a rising edge
// turn on LED
slope = 1; // 1 indicates rising edge
}
if ((slope == 1) && (val2 > threshold) && (val2 < va1)) // check for values after a prior rising edge, now falling edge
// turn off LED
slope = 0; // indicates falling edge
}
}
Thank you CrossRoads for the code i check it today and i give you information about how it`s works.
The red marker should turn On the diode and the blue marker should turn OFF the diode . I choose this way because the device caputring little changing in muscle contraction and the values oscillate in various values between 100 -300 .That's my idea but maybe you guys have another proposition .
I tried to compile your code CrossRoads but i have many errors. Im new in porgramming and i wrote only simple codes . I need to declare function like slope and val . I tried to do this but still have errors. My knowledge about programming is basic. Colud you show me how the codes should be looks like from the beggining to the end .
If anyone have any ideas how to write proper code . Please let me know.
slope and val are not functions, they are just variables.
Declare them before setup().
int val; // use for values from -32768 to 32767
byte slope; // use byte for values from 0 to 255
etc.
val1 and val2 will be in the range 0 to 1023 as they are from analogRead(), so int would be appropriate.
Thank for the code i upgrade it a little bit. Now when i flex the muscle the diode is turn ON but even when i relaxed muscle or flex it many times the diode still is ON it's never turn OFF. Maybe i do something wrong in the code . Now the code looks like this.
Thanks for your help.
const int ledPin=13; //
const int sensorPin= 0; //
int level; //
const int threshold1=400;//
const int threshold =500;
int va1;
int val1;
int val2;
byte slope;
void setup() {
pinMode (ledPin, OUTPUT); //
pinMode (sensorPin, INPUT); //
Serial.begin(9600);
//
}
void loop(){
val1 = analogRead(sensorPin);
val1 = analogRead (sensorPin); // gives ADC a chance to settle if source does not have much current drive
val2 = analogRead(sensorPin);
val2 = analogRead(sensorPin); // also enforces some time between readings, ~ 100-110uS
if ((slope == 0) && (val2 > threshold1) && (val2 > val1)) { // check for values after a prior falling edge, now a rising edge
// turn on LED
digitalWrite(ledPin, HIGH);
slope = 1; // 1 indicates rising edge
}
if ((slope == 1) && (val2 > threshold) && (val2 < va1)) // check for values after a prior rising edge, now falling edge
// turn off LED
digitalWrite(ledPin, LOW);
slope = 0; // indicates fallin
Hey guys with some help i wrote a need code but still the effect is not so good .
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;//