Wave machine servo control - Code issues

HI All

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;
}

Please say again or clarify.

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.

HTH

Oh, welcome to the forum.

a7

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 didn't notice the button wiring.

Here's your code with my changes and @Delta_G's advice applied in a different simulator

Something you might benefit from, I did when looking into whether the sketch needed to be so complicated, is adding

  Serial.begin(115200);

to the setup() function, and then using Serial.print() statements liberally to see the value of variables and the flow through your code.

HTH

a7

1 Like

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.

a7

Thanks to all for the reply's. Greatly appreciated and I now have it working.

Thanks again.

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.

yep, it works. I rewired and used the suggested changes from above.

# include <Servo.h>

const int buttonPin = 2;
const int servoPin = 9;

Servo myservo; 

void setup() {
  Serial.begin(115200);
  Serial.println("\n!\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 <= 80; pos += 1) {
        myservo.write(pos);
        delay(5);
      }
      for (int pos = 80; pos >= 0; pos -= 1) {
        myservo.write(pos);
        delay(5); 
      }
    }
  }
}
![Screenshot 2024-04-18 134436|690x292](upload://xj7boK3u68hsrHg4rDTG91ObsYo.png)

Then all I can say is that your physical layout diagram does not show how your system is wired up.

It is important you provide an accurate representation of your circuit. Something it looks like you have failed to do here.


This is what i have changed the diagram to and see the mock up photo as well.

It all works with the amended code.

1 Like

OK that would have been good to know. This now shows you have a common ground.

Nice. Simple is best.

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.

a7

Hello sheps75

Do you have a video showing the wave machine in real life?

I only count 3 * 1.5V = 4.5V nominal (4.8V max) alkaline batteries in that photo. Are they brand new?

Hi all

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.

Shep

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.