Go Down

Topic: Increasing two different servo PWM's with different delay's (Read 2 times) previous topic - next topic

flotilla


Quote

By now, you should know that you can't do this. You should not have ANY delays(). Look at the blink without delay example to see how to write delay()less code.


I do know this, hence the reason for asking for an alternative method. I dont really see how I could use 'millis' to help here, can you suggest how?.

My understanding is that Millis counts the time since the program started, however my program waits for inputs and runs other switch/modes before running my 'clocktime loop' 

The absolute simplest explanation I can think of what I trying to achieve' is:

Within one continous loop

every 60 seconds - increase servoA pwm by 4
every 120 seconds - increase servoB pwm by 3

looped forever, (or until reset)

I am trying to understand the code for timing/updating the pwm on the two servos within the same executed loop.

jraskell

The Blink Without Delay sample shows you how to time a particular event using millis().  It requires you track the required timing yourself, and execute the specific task when the appropriate amount of time has passed.

This is really the only method of timing multiple independent events.

wd40bomber7

#12
Jul 11, 2012, 10:42 pm Last Edit: Jul 11, 2012, 11:13 pm by wd40bomber7 Reason: 1


Quote

By now, you should know that you can't do this. You should not have ANY delays(). Look at the blink without delay example to see how to write delay()less code.


I do know this, hence the reason for asking for an alternative method. I dont really see how I could use 'millis' to help here, can you suggest how?.

My understanding is that Millis counts the time since the program started, however my program waits for inputs and runs other switch/modes before running my 'clocktime loop'  

The absolute simplest explanation I can think of what I trying to achieve' is:

Within one continous loop

every 60 seconds - increase servoA pwm by 4
every 120 seconds - increase servoB pwm by 3

looped forever, (or until reset)

I am trying to understand the code for timing/updating the pwm on the two servos within the same executed loop.


There are tons of different ways to do this, but here's a simple one for you.

Code: [Select]

int servoA = 0;
int servoB = 0;
int counter = 0;
void loop() {


delay(60);
servoA += 4;

counter++;

if (count == 2) {
servoB += 3;
counter=0;
}
}


Obviously instead of incrementing variables you'll have to increment the PWMs themselves.

Edit: Fixed a missing semicolon. I don't actually have the editor in front of me, so I just typed that up in notepad.

flotilla

Just wanted to thank you all for your patience and help.  I managed to get it working with the 'millis' method
as suggested. After staring at it for an hour it finally clicked into place

My code for that particular section is below for anyone who is interested (majority omitted).

Thanks Again!

Code: [Select]


(Setup declarations)

unsigned long currentMillisA = millis();  //Storage for Timer
unsigned long currentMillisB = millis();  //Storage for Timer

long previousMillisA = 0;        // Millis Counter Store
long previousMillisB = 0;        // Millis Counter Store

long StepperAinterval = 60000; // 60 seconds break time         
long StepperBinterval = 120000; // 120 seconds break time

(inside void loop())

case 7: // Commence Time
////////////

if (MinutePulse >= 2500) mode = 10; //servo safety to stop it over-turning
if (HourPulse >= 2500) mode = 10;
////////////
 
  if(CurrentMillisA - previousMillisA > StepperAinterval) {
    MinutePulse = MinutePulse + 4;
    previousMillisA = CurrentMillisA;
    MinuteHand.writeMicroseconds(MinutePulse);
    Serial.print("MINUTE PWM VALUE IS SET TO ");
    Serial.println(MinutePulse);
  }
  if(CurrentMillisB - previousMillisB > StepperBinterval) {
    HourPulse = HourPulse + 3;
    previousMillisB = CurrentMillisB;
    Serial.print("HOUR PWM VALUE IS SET TO ");
    Serial.println(HourPulse);
    HourHand.writeMicroseconds(HourPulse);
  }

   
break;

PaulS

Quote
Within one continous loop

every 60 seconds - increase servoA pwm by 4
every 120 seconds - increase servoB pwm by 3

I'm really curious why you want the motor to run faster and faster, instead of a constant speed. Every clock I own has a motor that runs at a constant speed. I can't help but think I am missing something.

Go Up