Hi i am new here,
i'm doing a robotic hand ,that can be control by using a push button and flex sensor.
The condition is like this
Here is my program for the push button it run perfectly
#include <Servo.h>
int button1 = A0;
int button2 = A1;
int button3 = A2;
int button4 = A3;
int button5 = A4;
int press1 = 0;
int press2 = 0;
int press3 = 0;
int press4 = 0;
int press5 = 0;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
myservo1.attach(2);
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
myservo1.write(179);
delay(100);
}
else{
myservo1.write(1);
delay(100);
}
{
press2 = digitalRead(button2);
if (press2 == LOW)
{
myservo2.write(179);
delay(100);
}
else{
myservo2.write(1);
delay(100);
}
{
press3 = digitalRead(button3);
if (press3 == LOW)
{
myservo3.write(179);
delay(100);
}
else{
myservo3.write(1);
delay(100);
}
{
press4 = digitalRead(button4);
if (press4 == LOW)
{
myservo4.write(179);
delay(100);
}
else{
myservo4.write(1);
delay(100);
}
{
press5 = digitalRead(button5);
if (press5 == LOW)
{
myservo5.write(179);
delay(100);
}
else{
myservo5.write(1);
delay(100);
}
}
}
}
}
}
Flex sensor,this also run perfectly
#include <Servo.h>
Servo myservo1;// create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
int potpin1 = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
Serial.begin(9600);
myservo1.attach(2); // attaches the servo on pin 9 to the servo object
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);
}
void loop()
{
val = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
Serial.println(val);
val = map(val, 50, 300, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(val);
myservo2.write(val);
myservo3.write(val);
myservo4.write(val);
myservo5.write(val);
// sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
The problem is when i combined it like this ,the servo keep moving.
#include <Servo.h>
int button1 = A0;
int button2 = A1;
int button3 = A2;
int button4 = A3;
int button5 = A4;
int potpin1 = A5; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int press1 = 0;
int press2 = 0;
int press3 = 0;
int press4 = 0;
int press5 = 0;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
Serial.begin(9600);
myservo1.attach(2);
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
myservo1.write(179);
delay(100);
}
else{
myservo1.write(1);
delay(100);
}
{
press2 = digitalRead(button2);
if (press2 == LOW)
{
myservo2.write(179);
delay(100);
}
else{
myservo2.write(1);
delay(100);
}
{
press3 = digitalRead(button3);
if (press3 == LOW)
{
myservo3.write(179);
delay(100);
}
else{
myservo3.write(1);
delay(100);
}
{
press4 = digitalRead(button4);
if (press4 == LOW)
{
myservo4.write(179);
myservo5.write(179);
delay(100);
}
else{
myservo4.write(1);
myservo5.write(1);
delay(100);
}
{
press5 = digitalRead(button5);
if (press5 == LOW)
{
val = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
Serial.println(val);
val = map(val, 50, 300, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(val);
myservo2.write(val);
myservo3.write(val);
myservo4.write(val);
myservo5.write(val);
// sets the servo position according to the scaled value
delay(15);
}
}
}
}
}
}