Problem with code?


hi new here,

i'm doing robotic hand ,that can control using push button , flex sensor.
the condition

here program push button run perfectly
code: [select]
#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 run perfectly
code: [select]
#include <servo.h>

servo myservo1;// create servo object control servo
servo myservo2;
servo myservo3;
servo myservo4;
servo myservo5;

int potpin1 = 0;  // analog pin used connect potentiometer
int val;    // variable read value analog pin

void setup()
{
  serial.begin(9600);
  myservo1.attach(2);  // attaches servo on pin 9 servo object
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
}

void loop()

{
  val = analogread(potpin1);            // reads value of potentiometer (value between 0 , 1023)
  serial.println(val); 
  val = map(val, 50, 300, 0, 179);     // scale use servo (value between 0 , 180)
  myservo1.write(val);
myservo2.write(val);
myservo3.write(val);
myservo4.write(val);
myservo5.write(val);
// sets servo position according scaled value 
  delay(15);                           // waits servo there



the problem when combined ,the servo keep moving.

code: [select]
#include <servo.h>

int button1 = a0;
int button2 = a1;
int button3 = a2;
int button4 = a3;
int button5 = a4;

int potpin1 = a5;  // analog pin used connect potentiometer
int val;    // variable read value 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 value of potentiometer (value between 0 , 1023)
  serial.println(val); 
  val = map(val, 50, 300, 0, 179);     // scale use servo (value between 0 , 180)
  myservo1.write(val);
myservo2.write(val);
myservo3.write(val);
myservo4.write(val);
myservo5.write(val);
// sets servo position according scaled value 
  delay(15);
}
}


}
}
  }

which servo keeps moving ?

what sort of buttons have ?


Arduino Forum > Using Arduino > Programming Questions > Problem with code?


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support