Pages: [1]   Go Down
Author Topic: Motor Acceleration Timer Deceleration Problem  (Read 1130 times)
0 Members and 1 Guest are viewing this topic.
Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am having some trouble getting the following code to work. Any Ideas?

Code:
//////////////////////////////

#define STIRRING1_TIME 20  //   1200
#define Srelay    51
int stir_speed = 180;
int time = 0;  
int pump_pwm_del = 1;
int second = 0;
void setup() {
  Serial.begin(57600);
  Serial.println("setup");

  digitalWrite(23,HIGH);
  
  /////////////////////////////
  pinMode(Srelay, OUTPUT); // Provides/Removes Power to motor controllers
  pinMode(44, OUTPUT); //Shared PWM Signal to all 4 motor controllers
  pinMode(23, OUTPUT);   //Stir motor
  digitalWrite(Srelay,LOW);//Turn off power to motor controllers
}

void loop() {
  digitalWrite(Srelay,HIGH);//Turn off power to motor controllers  
  Serial.println("23");
  stir(10);
 
}

/////////////////////////////////////////////////////////
//Stir
int stir(int time){
  digitalWrite(23,LOW); // Release Brake Stir Motor
     Serial.print("Brake On ");
  for (int motorspeed = 0; motorspeed < stir_speed; motorspeed++) {
    analogWrite(44, motorspeed);// Ramp up speed
     Serial.print("motorspeed = ");
    Serial.println(motorspeed);
    delay(4);
  }
  //motor at speed start counting
  Serial.print("motorspeed = MAX ");
  static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
  // (static variables are initialized once and keep their values between function calls)
  if (millis() - lastTick >= 1000) {
    
    lastTick = millis();
    second++;
  
    
    Serial.print("Second = ");
    Serial.println(second);

  
    if (second > time){   // Count over decel motor
      Serial.print("Time Out");
       for (int motorspeed = stir_speed; motorspeed >= 0; motorspeed--) {
    analogWrite(44, motorspeed);  //Ramp down
     Serial.print("motorspeed = ");
    Serial.println(motorspeed);
    delay(4);
  }
    Serial.print("motorspeed = MIN ");
  digitalWrite(23, HIGH);// Set Brake stir Motor
   Serial.print("Brake On ");
    }
  }
 
  delay(1000);
}

It is supposed to Ramp up and  do the timer code and then ramp down.

motorspeed = 176
motorspeed = 177
motorspeed = 178
motorspeed = 179
motorspeed = MAX 25
Brake On motorspeed = 0
motorspeed = 1
motorspeed = 2
motorspeed = 3
motorspeed = 4
motorspeed = 5
motorspeed = 6

« Last Edit: April 23, 2013, 02:29:49 pm by urthlight » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It is supposed to do the timer code and then ramp down.
Instead, it?

I fail to see the purpose of using millis() to create a non-blocking function and then using delay() in the same function.
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Instead, it?


motorspeed = 176
motorspeed = 177
motorspeed = 178
motorspeed = 179
motorspeed = MAX 25
Brake On motorspeed = 0
motorspeed = 1
motorspeed = 2
motorspeed = 3
motorspeed = 4
motorspeed = 5
motorspeed = 6

Quote
I fail to see the purpose of using millis() to create a non-blocking function and then using delay() in the same function.

The millis keeps track of time and the delay smooths out the accel/decel ramps
« Last Edit: April 23, 2013, 02:45:19 pm by urthlight » Logged

USA
Offline Offline
Jr. Member
**
Karma: 4
Posts: 89
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What happens if this statement is false?

Code:
  if (millis() - lastTick >= 1000)

Logged


East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4056
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This thread seems familiar ....

http://arduino.cc/forum/index.php/topic,160843.0.html
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yup, Still wrestling with this issue. I seem to get comments and criticisms instead of help.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25760
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I seem to get comments and criticisms instead of help
It's an understandable side-effect of cross-posting.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It's an understandable side-effect of cross-posting.
Actually I posted a new question because I was receiving critique instead of help. Seems there is no shortage of people to tell you you are doing it wrong, and only a few pathfinders.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25760
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe try reading the critiques, and applying the advice?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

USA
Offline Offline
Jr. Member
**
Karma: 4
Posts: 89
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

bigger hint:

Code:
  if (millis() - 0 >= 1000)

Logged


London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am having some trouble getting the following code to work. Any Ideas?

Code:
   if (second > time){   // Count over decel motor
      Serial.print("Time Out");
       for (int motorspeed = stir_speed; motorspeed >= 0; motorspeed--) {
 

Assuming stir_speed is a positive number, then motorspeed will always be >0 and your for loop won't do anything. That's why it's not ramping up. Try:
Code:
for (motorspeed = stir_speed; motorspeed <= 0; motorspeed--)
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Assuming stir_speed is a positive number, then motorspeed will always be >0 and your for loop won't do anything. That's why it's not ramping up. Try:

Unless I'm missing something, your proposal causes the problem you're describing.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
bigger hint:



 
Code:
if (millis() - 0 >= 1000)

I am confused by this code. I have it working elsewhere. It counts 1000 millisec and increments the second variable.
But, I think I am tracking that  millis() returns a rather large number (elapsed time) and - 0 it is still >= 1000.
So I need to initialize
Code:
static unsigned long lastTick = 0;
this differently?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25760
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So I need to initialize
Code:

 static unsigned long lastTick = 0;

this differently?
You don't need to initialise it at all - it is static, so the compiler makes it zero, unless you tell it otherwise.
But you do need to update it.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You don't need to initialise it at all - it is static, so the compiler makes it zero, unless you tell it otherwise.
But you do need to update it.

Code:
static unsigned long lastTick = 0;
So something more like
Code:
static unsigned long lastTick = millis();
Logged

Pages: [1]   Go Up
Jump to: