Software debouncing in a servo motor using myoware muscle sensor

Hi guys. So i got a code here for the software debouncing. Im using a myoware muscle sensor to move the servo. what i need to do is change the digitalreading to analog reading with a following limit.

so here’s the code that i got from the forum

#include <Servo.h>
int button = A3; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
pinMode(button, INPUT); //arduino monitor pin state

servo.attach(9); //pin for servo control signal
digitalWrite(12, HIGH); //enable pullups to make pin high
}

void loop()
{

press = digitalRead(button);

if (press == LOW)
{
if(toggle)
{
servo.write(160);
toggle = !toggle;
}

else
{
servo.write(60);
toggle = !toggle;
}
}

delay(1000); //delay for debounce
}

with a pushbutton it perfectly works. But what i need is for an analog value greater than 200 (button is pressed) please guide i really need this for my thesis.

It would seem to me that, in your research, you should have found analogRead() somewhere during the search. Second, please read the post at the top of this Forum by Nick Gammon on the proper way to post source code here using code tags. Finally, while researching analogRead() you may want to investigate map(), too.

Hello sorry for that. I have made this

pls confirm if its right

#include <Servo.h>
int button = A3; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
pinMode(button, INPUT); //arduino monitor pin state

servo.attach(9); //pin for servo control signal
digitalWrite(12, HIGH); //enable pullups to make pin high
}

void loop()
{

press = digitalRead(analogRead(button>300));

if (press == LOW)
{
if(toggle)
{
servo.write(160);
toggle = !toggle;
}

else
{
servo.write(60);
toggle = !toggle;
}
}

delay(1000); //delay for debounce
}

Still no code tags I see

  press = digitalRead(analogRead(button > 300));Make your mind up whether you are doing a digital or analogue read. One or the other, but not both would seem a good idea.

  digitalWrite(12, HIGH); //enable pullups to make pin highWhere does pin 12 come into it ?

  delay(1000);  //delay for debounceThat is a heck of a lot of contact bounce

UKHeliBob:
That is a heck of a lot of contact bounce

Oh...look! The cows came home!

press = digitalRead(analogRead(button>300));

You are asking for analogRead on the pin (button>300). Doesn't look like a valid pin number
You then ask for a digitalRead of the pin that the analogRead might return, which can be 0 to 1023. Too broad of a range for the Arduino. 0-19 for '328P, 0 to 31 for a '1284P, 0-69 for a Mega.

hey guys what id like to do is to make a digital reading of HIGH whenever the analog reading is above 300. pls help me

Read analog input, create a digital high output (“digital reading of HIGH” is not correct terminology) when the analog reading is > 300:

if (analogRead(A0)>300){
digitalWrite(pin2, HIGH);
// or someVariable = 1;
}
else {
digitalWrite (pin2, LOW);
// or someVariable = 0;
}

Hello so i did this. it doesnt work. the servo keeps on going from 60 to 160 continuously.

#include <Servo.h>
int button = A3; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
pinMode(button, INPUT); //arduino monitor pin state

servo.attach(9); //pin for servo control signal
analogWrite(A3, HIGH); //enable pullups to make pin high
}

void loop()
{
if (analogRead(A3)>300)

{
digitalWrite(press, HIGH);
press= HIGH;

}

// or someVariable = 1;

else {
digitalWrite (press, LOW);
press=LOW;

// or someVariable = 0;
}
if (press == LOW)
{
if(toggle)
{
servo.write(160);
toggle = !toggle;
}

else
{
servo.write(60);
toggle = !toggle;
}
}

delay(500); //delay for debounce
}

djlegendaryl:
Hello sorry for that. I have made this

pls confirm if its right

Why do you need us to confirm it? When you upload it to an arduino and test it, does it work or not?

  analogWrite(A3, HIGH); //enable pullups to make pin highYou cannot do an analogWrite() to a non PWM pin and in any case analogWrite(1) does not really make sense. What are you trying to do here ?

    digitalWrite(press, HIGH);This initially sets pin 0 HIGH. Why do you do that ?
Later it may set pin 1 HIGH depending on the value of the press variable. Why do that ?

  delay(500);  //delay for debounce

Still way too long for debounce

Try printing the value of key variables before the if tests in your program but get rid of the digitalWrite()s to press first or they will interfere with the Serial interface.

UKHeliBob:
  analogWrite(A3, HIGH); //enable pullups to make pin highYou cannot do an analogWrite() to a non PWM pin and in any case analogWrite(1) does not really make sense. What are you trying to do here ?

    digitalWrite(press, HIGH);This initially sets pin 0 HIGH. Why do you do that ?
Later it may set pin 1 HIGH depending on the value of the press variable. Why do that ?

  delay(500);  //delay for debounce

Still way too long for debounce

Try printing the value of key variables before the if tests in your program but get rid of the digitalWrite()s to press first or they will interfere with the Serial interface.

It was solved already thanks to mr corss roads

here is the code

#include <Servo.h>
byte button = A3; //button pin, connect to ground to move servo
byte press = 0;
byte pressState = 0;
Servo servo;
byte toggle = 0;

void setup()
{
pinMode(button, INPUT_HIGH); //arduino monitor pin state

servo.attach(9); //pin for servo control signal
// analogWrite(A3, HIGH); //enable pullups to make pin high
}

void loop()
{
if (digitalRead(button)==LOW)
{
digitalWrite(press, HIGH);
pressState = HIGH;
}

else {
digitalWrite (press, LOW);
pressState=LOW;
}
if (pressState == LOW)
{
if(toggle==1)
{
servo.write(160);
toggle = 1 - toggle;
}

else
{
servo.write(60);
toggle = 1 - toggle;
}
}

delay(500); //delay for debounce
}

It was solved already

But still you are not using [­code] [­/code] tags when posting code

UKHeliBob:
But still you are not using [­code] [­/code] tags when posting code

CrossRoads:
Read analog input, create a digital high output ("digital reading of HIGH" is not correct terminology) when the analog reading is > 300:

if (analogRead(A0)>300){

digitalWrite(pin2, HIGH);
// or someVariable = 1;
}
else {
digitalWrite (pin2, LOW);
// or someVariable = 0;
}

CrossRoads:
Read analog input, create a digital high output ("digital reading of HIGH" is not correct terminology) when the analog reading is > 300:

if (analogRead(A0)>300){

digitalWrite(pin2, HIGH);
// or someVariable = 1;
}
else {
digitalWrite (pin2, LOW);
// or someVariable = 0;
}

CrossRoads:
Read analog input, create a digital high output ("digital reading of HIGH" is not correct terminology) when the analog reading is > 300:

if (analogRead(A0)>300){

digitalWrite(pin2, HIGH);
// or someVariable = 1;
}
else {
digitalWrite (pin2, LOW);
// or someVariable = 0;
}

Hello Mr cross roads. You have sent me a working code. but what if i want an analog signal above 300 as substitute for the on state?