Newbie here. I am making a wave machine to use with Marine students. I have a micro servo pushing a plate in the water, and i want it to repeat 3 times, once i push a button controller. I have modelled the hardware and code in tinkercad, but it doesn't work. Can someone run an eye over this and let me know where i am going wrong?
Capacitors are 100uf and the power supply is rated to 1 amp.
Thanks
#include <Servo.h>
const int buttonPin = 2; // Replace with your button pin number
const int servoPin = 9; // Replace with your servo pin number
Servo myservo; // Create a servo object
int buttonState = 0; // Current button state
int lastButtonState = 0; // Previous button state
int cycleCount = 0; // Counter for cycles
void setup() {
pinMode(buttonPin, INPUT);
pinMode(servoPin, OUTPUT);
myservo.attach(servoPin); // Attach the servo to the pin
}
void loop() {
// Read the pushbutton state
buttonState = digitalRead(buttonPin);
// Debounce the button press (optional)
if (buttonState != lastButtonState) {
delay(50); // Wait for the button to settle
buttonState = digitalRead(buttonPin);
}
// Check if the button is pressed and cycleCount is less than 3
if (buttonState == LOW && cycleCount < 3) {
for (int pos = 0; pos <= 60; pos += 1) {
myservo.write(pos); // Move the servo to 60 degrees
delay(15); // Delay for movement
}
for (int pos = 60; pos >= 0; pos -= 1) {
myservo.write(pos); // Move the servo back to 0 degrees
delay(15); // Delay for movement
}
cycleCount++; // Increment cycle count
}
// Update the lastButtonState for debouncing
lastButtonState = buttonState;
}
Do you want three wave push cycles each time you press the button?
You never reset cycleCount that I see, so there's one problem.
And it's hard to see without running the sketch if you button logic works, since it is spread out with the wave cycler in the middle of it.
Assuming you want three wave cycles per press, the low rent immediate solution is to simply condition the wave cycler on seeing the button.
You'd just have to get you skinny finger off the button before the three cycles finished
if the button is seen to be pressed
loop three times
doing a slow move out then
doing a slow move back
An if statement with a for loop in it that has two for loops.
Next step would be to debounce and detect when the button gets pressed, this would remove the requirement that the operator stab the button rather than hold it down beyond the end of the three-cycle.
When your button isn't pressed, the input it is connected to is floating. The normal way to wire a button is to put it between the pin and ground, instead of 5V like you have it now. Then you change your pinMode to INPUT_PULLUP and you will get LOW for a pressed button and HIGH when not pressed.
I'm posting the code; something strange is going on here and all I get when I click the link in my previous post is an intermediate vesrion from I do not know where.
# include <Servo.h>
const int buttonPin = 2;
const int servoPin = 9;
Servo myservo;
void setup() {
Serial.begin(115200);
Serial.println("\nJello Whirled!\n");
pinMode(buttonPin, INPUT_PULLUP);
myservo.attach(servoPin);
myservo.write(0);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
Serial.println("button would let this if");
for (int ii = 0; ii < 3; ii++) {
Serial.print("wave ");
Serial.println(ii + 1);
for (int pos = 0; pos <= 60; pos += 1) {
myservo.write(pos);
delay(15);
}
for (int pos = 60; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
}
}
I did also remove gratuitous and incorrect or misleading comments.
added: yup, even on a different 'puter I get from the link in #4 a half baked version that does not work. Please just paste this code all over the code in the simulation, the diagram.json is the same.
It's too tired and I'm getting late to run this odd thing to ground just now.
Have you?
According to that diagram not only the push button was wired incorrectly but also the external power supply providing power to the motor did not have the negative of the supply connected to the negative of the Arduino.
If this is a simulation only this might not show up, but when it is for real it will fail.
Those dinky servos can use all of 4x AA batteries. The 4.8 - 6.0 volt specification comes, I think, from the nominal voltage of four NiCad cells in series to 4 regular AA cells in series.
What is to be the actual servo for the real wave maker?
Is there ever to be a more involved process to control? Just curious.
IThanks for the feedback. have changed it up to use a 5V power pack and I will post up a video of the wave machine once i have it completed. Just 3d Printing some mounting hardware ATM.