Hi there i am having an issue with a servo control. I want wen i press a button to increase 10degrees and store it.
I load it just fine on an arduino uno but when i run it the servo goes crazy.
void loop()
{
buttonState1 = digitalRead(signal1); //enables signal read from pin 0v or 5v
buttonState2 = digitalRead(signal2); //enables signal read from pin 0v or 5v
if (buttonState1 == HIGH) {
pos1= pos1+10;
servo1.write(pos1);
}
else {
servo1.write(pos1);
}
Serial.println(pos1);
The rest of the code is to big with plenty of if's because i have 8 button 2 for each servo. 1 for increase 10 degrees and 1 to decrease 10 degrees for each servo. I don't get any error at compilation just don't reacts as i want. Already made all the statements but ony one servo in the Void loop. If the first works the rest will be done easily.
Servos attached to 6,9,10,11 and buttont to 2,3,4,5,7,8,12,13
MMA7361 attached to A0,A1,A2.
For now i want the servos to work then i'll try to make the code for the MMA7361
also i have int resPos = 40; as a reset possition
Here is the hole code. And what do you mean Debug? any pdf arduino turtorial for me to understand the IDE not with examples. i can see many examples but i can't understand everything.
#include <Servo.h>
Servo servo1, servo2, servo3, servo4; // create servo object to control a servo
int resPos = 40;
int pos1 = 40; // variable to store the servo position
int pos2 = 40; // variable to store the servo position
int pos3 = 40; // variable to store the servo position
int pos4 = 40; // variable to store the servo position
const int signal1 = 2; //input signal high or low
const int signal2 = 3; //input signal high or low
const int signal3 = 4; //input signal high or low
const int signal4 = 5; //input signal high or low
const int signal5 = 7; //input signal high or low
const int signal6 = 8; //input signal high or low
const int signal7 = 12; //input signal high or low
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;
void setup()
{
servo1.attach(6); // attaches the servo on pin 6 to the servo object
servo2.attach(9); // attaches the servo on pin 9 to the servo object
servo3.attach(10); // attaches the servo on pin 10 to the servo object
servo4.attach(11); // attaches the servo on pin 11 to the servo object
pinMode(signal1,INPUT); //defines pin as input
pinMode(signal2,INPUT); //defines pin as input
pinMode(signal3,INPUT); //defines pin as input
pinMode(signal4,INPUT); //defines pin as input
pinMode(signal5,INPUT); //defines pin as input
pinMode(signal6,INPUT); //defines pin as input
pinMode(signal7,INPUT); //defines pin as input
Serial.begin(9600);
}
void loop()
{
buttonState1 = digitalRead(signal1); //enables signal read from pin 0v or 5v
buttonState2 = digitalRead(signal2); //enables signal read from pin 0v or 5v
buttonState3 = digitalRead(signal3); //enables signal read from pin 0v or 5v
buttonState4 = digitalRead(signal4); //enables signal read from pin 0v or 5v
buttonState5 = digitalRead(signal5); //enables signal read from pin 0v or 5v
buttonState6 = digitalRead(signal6); //enables signal read from pin 0v or 5v
buttonState7 = digitalRead(signal7); //enables signal read from pin 0v or 5v
if (buttonState1 == HIGH) {
pos1 = pos1+10;
servo1.write(pos1);
}
else if (buttonState2 == HIGH) {
pos1 = pos1-10;
servo1.write(pos1);
}
else {
servo1.write(pos1);
}
Serial.println(pos1);
}
AWOL:
That's because you're incrementing all the time the switch is pressed, not when it becomes pressed., ie when it goes from not pressed to pressed.
what that suppose to mean? NNOOOOOBB here sorry. The examples are nice at all the turtorials but when it comes to a code creation
When you hold the button down the position variable increases each time through the loop() function. What you need to do is only increment the position when the button state changes from not pressed to pressed and not increment it if the button is held down.
Look at the state change example in the IDE to see how to do it. Basically you remember the state of the button the last time that it was read and only take action when it has changed, ie has been pressed.
#include <Servo.h>
Servo servo1, servo2, servo3, servo4; // create servo object to control a servo
int resPos = 40;
int pos1 = 40; // variable to store the servo position
int pos2 = 40; // variable to store the servo position
int pos3 = 40; // variable to store the servo position
int pos4 = 40; // variable to store the servo position
const int signal1 = 2; //input signal high or low
const int signal2 = 3; //input signal high or low
const int signal3 = 4; //input signal high or low
const int signal4 = 5; //input signal high or low
const int signal5 = 7; //input signal high or low
const int signal6 = 8; //input signal high or low
const int signal7 = 12; //input signal high or low
int buttonState1 = 0; // current state of the button
int buttonState2 = 0; // current state of the button
int buttonState3 = 0; // current state of the button
int buttonState4 = 0; // current state of the button
int buttonState5 = 0; // current state of the button
int buttonState6 = 0; // current state of the button
int buttonState7 = 0; // current state of the button
int lastButtonState1 = 0; // previous state of the button
int lastButtonState2 = 0; // previous state of the button
int lastButtonState3 = 0; // previous state of the button
int lastButtonState4 = 0; // previous state of the button
int lastButtonState5 = 0; // previous state of the button
int lastButtonState6 = 0; // previous state of the button
int lastButtonState7 = 0; // previous state of the button
void setup()
{
servo1.attach(6); // attaches the servo on pin 6 to the servo object
servo2.attach(9); // attaches the servo on pin 9 to the servo object
servo3.attach(10); // attaches the servo on pin 10 to the servo object
servo4.attach(11); // attaches the servo on pin 11 to the servo object
pinMode(signal1,INPUT); //defines pin as input
pinMode(signal2,INPUT); //defines pin as input
pinMode(signal3,INPUT); //defines pin as input
pinMode(signal4,INPUT); //defines pin as input
pinMode(signal5,INPUT); //defines pin as input
pinMode(signal6,INPUT); //defines pin as input
pinMode(signal7,INPUT); //defines pin as input
Serial.begin(9600);
}
void loop()
{
buttonState1 = digitalRead(signal1); //enables signal read from pin 0v or 5v
buttonState2 = digitalRead(signal2); //enables signal read from pin 0v or 5v
buttonState3 = digitalRead(signal3); //enables signal read from pin 0v or 5v
buttonState4 = digitalRead(signal4); //enables signal read from pin 0v or 5v
buttonState5 = digitalRead(signal5); //enables signal read from pin 0v or 5v
buttonState6 = digitalRead(signal6); //enables signal read from pin 0v or 5v
buttonState7 = digitalRead(signal7); //enables signal read from pin 0v or 5v
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
pos1 = pos1+10;
servo1.write(pos1);
}
else {
servo1.write(pos1);
}
Serial.println(pos1);
}}