PaulS has helped me with a code for saving and recalling servo positions with two pushbuttons, so many kudos to him!!
Ive tried uploading the code, connected the buttons to the +5v supply on the chip and put them back into analogue pins 4 & 5 on the UNO
but nothing seems to be happening... any ideas?
#include "Bounce.h"
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
int potpin5 = 4;
int potpin6 = 5;
Bounce A(potpin5, 10); // Substitute valid value for pinA
Bounce B(potpin6, 10); // Substitute valid value for pinB
int countA = 0;
int countB = 0;
const int servoCount = 4; // however many servos you have
int servoPos[servoCount]; // Array to hold servo positions
int potpin = 0; // analog pin used to connect the potentiometer
int val1; // variable to read the value from the analog pin
int potpin2 = 1; // analog pin used to connect the potentiometer
int val2; // variable to read the value from the analog pin
int potpin3 = 2; // analog pin used to connect the potentiometer
int val3; // variable to read the value from the analog pin
int potpin4 = 3; // analog pin used to connect the potentiometer
int val4; // variable to read the value from the analog pin
void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 10 to the servo object
myservo3.attach(5); // attaches the servo on pin 11 to the servo object
myservo4.attach(6); // attaches the servo on pin 12 to the servo object
}
void loop()
{
A.update();
B.update();
if(A.read() == HIGH) // Make this LOW is using internal pullups
{
// Save
servoPos[0] = val1;
servoPos[1] = val2;
servoPos[2] = val3;
servoPos[3] = val4; // Save the current servo positions in servoPos array
}
if(B.read() == HIGH && countA > 0) // Make this LOW is using internal pullups
{
countB++;
}
if(countB % 2 == 1 && countA > 0)
{
//Restore
myservo1.writeMicroseconds(servoPos[0]);
myservo2.writeMicroseconds(servoPos[1]);
myservo3.writeMicroseconds(servoPos[2]);
myservo4.writeMicroseconds(servoPos[3]); // Set servos to saved positions...
}
else
{
val1 = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val1 = map(val1, 0, 1023, 1500, 2425); // scale it to use it with the servo (value between 0 and 180)
myservo1.writeMicroseconds(val1); // sets the servo position according to the scaled value
// waits for the servo to get there
val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 1500, 2425); // scale it to use it with the servo (value between 0 and 180)
myservo2.writeMicroseconds(val2); // sets the servo position according to the scaled value
val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
val3 = map(val3, 0, 1023, 1500, 2425); // scale it to use it with the servo (value between 0 and 180)
myservo3.writeMicroseconds(val3); // sets the servo position according to the scaled value
val4 = analogRead(potpin4); // reads the value of the potentiometer (value between 0 and 1023)
val4 = map(val4, 0, 1023, 1500, 2425); // scale it to use it with the servo (value between 0 and 180)
myservo4.writeMicroseconds(val4); // sets the servo position according to the scaled value
delay(15); // Set servos to pot-controlled positions...
}
}
ok i see your point...
this is what i have so far it compiles but when uploaded the servos just jitter an awful lot but are still able to be controlled by the pots . any ideaS?
#include "Bounce.h"
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
const int buttoninput = 12;
const int buttoninput2 = 13;
Bounce A(buttoninput, 12); // Substitute valid value for pinA
Bounce B(buttoninput2, 13); // Substitute valid value for pinB
int countA = 0;
int countB = 0;
const int servoCount = 4; // however many servos you have
int servoPos[servoCount]; // Array to hold servo positions
int potpin = 0; // analog pin used to connect the potentiometer
int val1; // variable to read the value from the analog pin
int potpin2 = 1; // analog pin used to connect the potentiometer
int val2; // variable to read the value from the analog pin
int potpin3 = 2; // analog pin used to connect the potentiometer
int val3; // variable to read the value from the analog pin
int potpin4 = 3; // analog pin used to connect the potentiometer
int val4; // variable to read the value from the analog pin
void setup()
{
pinMode(13, INPUT);
pinMode(12, INPUT);
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 10 to the servo object
myservo3.attach(5); // attaches the servo on pin 11 to the servo object
myservo4.attach(6); // attaches the servo on pin 12 to the servo object
}
void loop()
{
{
A.update();
B.update();
if(A.read() == HIGH) // Make this LOW is using internal pullups
{
servoPos[0] = val1;
servoPos[1] = val2;
servoPos[2] = val3;
servoPos[3] = val4;// Save the current servo positions in servoPos array
}
if(B.read() == HIGH && countA > 0) // Make this LOW is using internal pullups
{
countB++;
}
if(countB % 2 == 1 && countA > 0)
{
myservo1.writeMicroseconds(servoPos[0]);
myservo2.writeMicroseconds(servoPos[1]);
myservo3.writeMicroseconds(servoPos[2]);
myservo4.writeMicroseconds(servoPos[3]); // Set servos to saved positions...
}
else
{
val1 = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val1 = map(val1, 0, 1023, 1500, 2425); // scale it to use it with the servo (value between 0 and 180)
myservo1.writeMicroseconds(val1); // sets the servo position according to the scaled value
// waits for the servo to get there
val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 1500, 2425); // scale it to use it with the servo (value between 0 and 180)
myservo2.writeMicroseconds(val2); // sets the servo position according to the scaled value
val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
val3 = map(val3, 0, 1023, 1500, 2425); // scale it to use it with the servo (value between 0 and 180)
myservo3.writeMicroseconds(val3); // sets the servo position according to the scaled value
val4 = analogRead(potpin4); // reads the value of the potentiometer (value between 0 and 1023)
val4 = map(val4, 0, 1023, 1500, 2425); // scale it to use it with the servo (value between 0 and 180)
myservo4.writeMicroseconds(val4); // sets the servo position according to the scaled value
delay(15); // Set servos to pot-controlled positions...
}
}
}
How are the switches connected? Are you using pull-down resistors?
Add Serial.begin(), with appropriate value, to setup(), and Serial.print() statements to loop(), to see when the switches are recognized as pressed (if ever), and the value in countA and countB. Add others as needed to see what is happening in the code.