Go Down

Topic: My counting method to control a servo does not loop; not sure how to fix (Read 488 times) previous topic - next topic

Preezus

Hello all!

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:

Code: [Select]

#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

  }
}

PaulS

Code: [Select]
    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
Code: [Select]
   myservo.write(180);
Why are you using a for loop to do that?

Quote
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?
The art of getting good answers lies in asking good questions.

Preezus

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...

DKWatson

Read your own post and think about it. You want to blink the LED 9 times,
Code: [Select]
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);
}

Done.
Now you want to move the servo.
Code: [Select]
myservo.write(pos);
Done. Now loop, but before you do, change pos.
Code: [Select]
if(pos == 0)
{
    pos = 1;
}
else
{
    pos = 0;
}

Or use the ternary statement,
Code: [Select]
pos = (pos == 0)?1:0;
Or simply negate,
Code: [Select]
pos = !pos;
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

PaulS

Quote
Or simply negate,
That would be challenging if the positions to move to are 60 and 90. Or 0 and 180.
The art of getting good answers lies in asking good questions.

Preezus

I implemented Watson's code.

The LED blinks constantly now without stopping, but the servo still only changes position after the first 9 blinks, not with blink 18, blink 27....

PaulS

Quote
I implemented Watson's code.
But failed to post it.

Quote
The LED blinks constantly now without stopping
Well, that is what it should do.

Quote
but the servo still only changes position after the first 9 blinks, not with blink 18, blink 27....
Then I think it is safe to assume you did something wrong.
The art of getting good answers lies in asking good questions.

Preezus

Sorry about that, Paul.

Code: [Select]

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

AWOL

Can you really see the difference.on a servo between 0 "degrees" and 1 "degree"?

Maybe "  myservo.write(pos * 180);"

Preezus


DKWatson

Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Preezus

How could I use a push button to control the counter here?

Code: [Select]

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

DKWatson

What? You want the flash to continue, push a button a move the servo?
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Preezus

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.

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

Go Up