I am seeking help with my program. My goal is to count a blinking LED to a desired amount (in this case 9 times) and then trigger a servo motor with one arm to sweep from 0 degrees to 180 degrees without delay and remain in that 180 degree position until another count of 9 is achieved. So basically I would like my program to loop back and forth and have the servo sweep back and forth but only after 9 blinks of an LED using a count. Here is the code:
#include <Servo.h>;
int ledPin = 13; // LED connected to digital pin 13
const int servoPin = 9; // Servo connected to digital pin 9
Servo myservo;
int maxnum1 = 9; // Blink the LED
int count = 0; // Our blink counter
int pos = 0; // first position of servo arm
int pos2 = 1; //second position of servo arm
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(servoPin, OUTPUT);
myservo.attach(9);
}
void loop()
{
if (count > 8)
{
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos2); // tell servo to go to position in variable 'pos2'
}
}
if (count < maxnum1) {
digitalWrite(ledPin, HIGH); // set the LED on
delay(500); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(499); // wait for a second
count++; // add one (1) to our count
}
}
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos2); // tell servo to go to position in variable 'pos2'
}
All that is code accomplishes is
myservo.write(180);
Why are you using a for loop to do that?
So basically I would like my program to loop back and forth and have the servo sweep back and forth but only after 9 blinks of an LED using a count.
What does it currently do, and how does that differ from what you want?
Currently, the program does not repeat. I would like it to repeat.
The LED blinks 9 times and then the servo motor is activated to position 2. Then the program ends.
I would like it to blink 9 times, activate servo motor to position 2, blink 9 times, activate servo to position 1, blink 9 times, activate servo motor to position 2, and so on...
Read your own post and think about it. You want to blink the LED 9 times,
for (byte i = 1; i <= 9; i++)
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(500); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(499);
}
void loop() {
for (byte i = 1; i <= 9; i++)
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(500); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(499);
}
myservo.write(pos);
if (pos == 0)
{
pos = 1;
}
else
{
pos = 0;
}
}
How could I use a push button to control the counter here?
for (byte i = 1; i <= 5; i++)
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(500); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(499);
}
I used the flash as more of an identifier for the test program. It is not required in what I am trying to accomplish, but does add a nice aesthetic touch.
I would like the push button to move the servo after 5 clicks.
How could I use a push button to control the counter here?
The for loop controls the index. Do you want to use the switch to change the upper limit? From what to what? When the switch IS pressed? Or, when the switch BECOMES pressed?
What have you tried? Posting here is the least effective method of achieving your goal. Reading the documentation and experimenting is far better and faster.
UKHeliBob:
Do you want to count the number of button presses, change the count using button presses or start counting when the button becomes pressed ?
I would like to count the number of button presses.
UKHeliBob:
What do you want to do with the count of button presses and how do you want to change from counting the button presses to blinking the LED ?
After a certain amount of presses (5 presses), I want to activate the servo arm 90 degrees. and then after another certain amount of presses (5 more presses) activate the arm back to zero degrees. I want this to keep going until power is disconnected
Eventually, I want to switch out the button for a sensor that will count parts. That is why I think I need to count the parts. It is just easier for me to connect and test the program with a button first, and then add the sensor once I know the program is working as intended.