Second day with Nano, need some advice

Hi guys, second day playing with my new Nano and would like some advice if you please.

I’m making a servo cycler/tester.

Currently I have it set up to start cycling the servo, serial print the number of cycles, then cycle again. It works as it should but I’d like to add the ability for me to push a button, have it start cycling the servo, and after x number of cycles (100, 300, 50, 1000, whatever you want) have it stop until I push the button again to start over.

I also have it printing the pushbutton state just so I can see. That will not be there when it’s all said and done, it was more made so I could know that the pushbutton was doing what it was supposed to and I just haven’t seen the need to remove it from the code yet.

Here’s what I have so far. Any tips on getting it to do what I want as I said above?

#include <Servo.h>
const int pinButton = 2;
Servo myservo;
int x = 0;


void setup() {
  Serial.begin(300);
  myservo.attach(9); 
  pinMode (pinButton, INPUT);
      
}

void loop() {
    
    int stateButton = digitalRead(pinButton);
    if (stateButton==HIGH){
   Serial.println(stateButton);
    myservo.write(10);
    delay(750);
    myservo.write(170);
   x++;
   Serial.println(x);
    delay(750);
    }
    else
    {
      
    }
}

Some day I’d like to be able to change this so that it prints to a LCD and have two push buttons to let the user increase/decrease the number of cycles by increments of 10 or 50, then push a button and let it begin. Then I’d add to the code where “else” would have it print to the LCD “Push Go To Start Test” or the like.

But that’s later, once I figure out this part :wink:

thanks in advance :smiley:

You need to look at the state change detection example. When the switch BECOMES pressed, you want to cycle the servo n times, using a for loop. You could have different switches loop different numbers of times.

Go to the reference page in 'Learning' above and look up for loops.

KpilotRCHelis:
I also have it printing the pushbutton state just so I can see. That will not be there when it's all said and done, it was more made so I could know that the pushbutton was doing what it was supposed to and I just haven't seen the need to remove it from the code yet.

That is very sensible. It would be great if everyone with queries thought of doing that.

...R