Hi all, Want to rotate multiple servos one by one with pressing single push button each time. Problem is that first servo is moving continuously unless push button is pressed by second time. Want to rotate servos one by one by clicking single push button. need help in coding. Thanks in advance.
Here is my code :
`
int switchPin = 13;
int val;
int val2;
int buttonState;
int Mode = 0;
void loop(){
val = digitalRead(switchPin);
delay(10);
val2 = digitalRead(switchPin);
if (val == val2) {
if (val != buttonState) {
if (val == LOW) {
if (Mode == 0) {
Mode = 1;
} else {
if (Mode == 1) {
Mode = 2;
} else {
if (Mode == 2) {
Mode = 3;
} else {
if (Mode == 3) {
Mode = 0;
}
}
}
}
}
}
buttonState = val;
switch (Mode) {
case 0:
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
delay (500);
break;
case 1:
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
delay (500);
digitalWrite(motor1, LOW);
break;
case 2:
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
digitalWrite(motor3, LOW);
delay (500);
digitalWrite(motor2, LOW);
break;
case 3:
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
digitalWrite(motor3, HIGH);
delay (500);
digitalWrite(motor3, LOW);
break;
}
default:
break ;
} `
abdul144:
Here is my code :
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frustrating for you and frustrating for us.
The people who try to help with your pro…
That is not complete code and does not compile. And what sort of servo is controlled with digitalWrites?
Steve
gcjr
November 6, 2021, 8:59am
4
doesn't look like your code is controlling a servo. A servo is controlled by a 1-2 msec pulse . the posted code simply turns a pin on for 500 msec.
the posted code also doesn't have a delay for the pin being off.
the button logic is also confused. button switches are normally connected between a pin and ground and the pin configured with an internal pullup resistor, INPUT_PULLUP. pressing the button makes the pin go LOW.
consider the following which flashed 1 of 3 LEDs. (Arduino pin 13 is an LED).
const byte mot1Pin = 13;
const byte mot2Pin = 12;
const byte mot3Pin = 11;
const byte motPins [] = { mot1Pin, mot2Pin, mot3Pin };
const byte butPin = A1;
byte butState;
enum { Off = HIGH, On = LOW };
#define N_MODE 4
int mode = 0;
// -----------------------------------------------------------------------------
void
motOff (void)
{
for (unsigned n = 0; n < sizeof(motPins); n++)
digitalWrite (motPins [n], Off);
}
// -----------------------------------------------------------------------------
#define Period 200
void
pulse (
byte motPin )
{
digitalWrite (motPin, On);
delay (Period);
motOff ();
delay (Period);
}
// -----------------------------------------------------------------------------
void
loop (void)
{
byte but = digitalRead (butPin);
if (butState != but) {
butState = but;
delay (10); // debounce
if (LOW == but) {
mode++;
if (N_MODE < mode)
mode = 0;
}
}
switch (mode) {
case 1:
pulse (mot1Pin);
break;
case 2:
pulse (mot2Pin);
break;
case 3:
pulse (mot3Pin);
break;
default:
motOff ();
break;
}
}
// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin (9600);
pinMode (butPin, INPUT_PULLUP);
butState = digitalRead (butPin);
for (unsigned n = 0; n < sizeof(motPins); n++) {
pinMode (motPins [n], OUTPUT);
digitalWrite (motPins [n], Off);
}
}
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy for forum"
paste clipboard into write-window of a posting
best regards Stefan
system
Closed
May 5, 2022, 10:32am
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.