count the push buttons

Hello together,

I don't know how I can realize my idea. I want to control 4 servos. For example I use output 9, 10, 11, 12. Okay and then I want to push the button (Input 0) for choicing the servo
which are on output 9, 10, 11, 12. Parallel to this I have one poti (Input 1) to control my different servos (9, 10, 11, 12).

Again: If I push the button one time I want to use output 9. If I have pushed 4 times the button I will use output 12. After the forth time I want to count my button pushes again (1 (Output 9), 2 (Output 10), 3(Output 11) and 4(Output 12)).

I hope you understand what I mean and you can give the code for this.

Thanks a lot :slight_smile:

Flitzer09

We don't give code here. Not complete sketches. What problems are you having, putting this idea together?

Okay well, but can you give me pseudo code.

In the first instance I used 4 servos and 4 potis, but there is a problem the servos have an jitter.

For that I have the code, but now I want to try it like I had described, do you can give me pseudo code, if you don't want to give me code.

Thanks... :slight_smile:

Flitzer09:
Okay well, but can you give me pseudo code.

That's not how it works... You give us (pseudo) code and we go from there.

Flitzer09:
In the first instance I used 4 servos and 4 potis, but there is a problem the servos have an jitter.

For that I have the code, but now I want to try it like I had described, do you can give me pseudo code, if you don't want to give me code.

And you thought, let's just not post it?

You can try the below approach.

  1. When the first button press. Enable a flag to true. Also increment a counter.
  2. Another function will look for the above flag to true, if true, execute the code based on the counter.
  3. The above function should use millis and wait for say 1 second before switching on a motor. The motor to switched on is based on the counter. Waiting for 1 sec is to allow the user to press the button 1 to 4 times. After 1 sec the function will conclude that user pressed the button based on his requirement.

Thanks for the info and the pseudo I will try it :slight_smile:

Hi,
Welcome to the forum.

In the first instance I used 4 servos and 4 potis, but there is a problem the servos have an jitter.

How have you got your servos powered, you arduino cannot provide enough current to drive the number you have.
You will need a separate supply for your servos.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please not a fritzy diagram.

Thanks.. Tom.. :slight_smile:

Because of that I have 4 servos I want to control only 1 servo after every pushed buttons.

In the first instance I tried a 2A and 5V supply, but I have also jitters.

My servo are the sg90 ,the small ones for arduino and raspberry pi. To control every servo (4) to control with 4 different servos were a bad idea, it doesn't function.

when you press to select 9, then adjust the pot. the output goes to #9

now, when you want to select 10, whatever the pot was, will be the signal for 10.
9 is either going to get no signal or you will have create a way to hold the last signal

Here my circuit

Here is the code, I want to use, but it doesn't function. ledPin was used in the program before, which I found in the internet. I made small changes, e.g. the choice of the different servos. And I thought, that the modulo by using the pushbuttons would solve my problems, nothing... I need your help, please...

#include <Servo.h>

const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

Servo myservo9;
Servo myservo10;
Servo myservo11;

int potpin = 0;
int potpin2 = 2;
int potpin3 = 5;

int val = 0;
int val2 = 0;
int val3 = 0;

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);

myservo9.attach(9);
myservo10.attach(10);
myservo11.attach(11);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 3 == 0) {
val = analogRead(potpin);
val = map(val, 3, 1023, 0, 176);
myservo9.write(val);
delay(25);
} //else {
//digitalWrite(myservo9, LOW);
//}

if (buttonPushCounter % 3 == 1) {
val2 = analogRead(potpin);
val2 = map(val2, 3, 1023, 0, 176);
myservo10.write(val2);
delay(25);
} // else {
// digitalWrite(myservo10, LOW);
// }
if (buttonPushCounter % 3 == 2) {
val3 = analogRead(potpin);
val3 = map(val3, 3, 1023, 0, 176);
myservo10.write(val3);
delay(25);
} //else {
//digitalWrite(myservo11, LOW);
// }

}