Go Down

Topic: PWM motor speed question (Read 1 time) previous topic - next topic


Suspicious of what?

Redundant states. If you have a lot of states representing similar activities in different contexts, perhaps you would be better to consolidate them into one set of states which has parameters which specify the context.

There are also ways to structure the state machine that reduce the code and data replication between states and make each state more self contained. One that works well is to have the handler function for each state return the new state, and have your state machine framework compare old/new states to detect state transitions. You can use that to trigger exit processing for the old state and entry processing for the new state. For example actions such as initialising sub-state variables such as motor speed and run duration, and starting the motor, could be handled by state entry code. This is a good place to put logging, too. Then the processing for each state would be reduced to 'if the motor speed has reached the target, move to the next state' and so on. It may not apply here, but this approach means that entry/exit processing for each state is always done reliably, without any code replication, regardless of how complex the sequences of state transitions can become. If you need to increase the complexity even further, it gives you a hook which you can use to manage compound finite state machines.
I only provide help via the forum - please do not contact me for private consultancy.


This sounds good for rev 2. It will take a lot of consideration. And yeah the original plan called for 12 states but it evolved. There are redundant states that could have values passed to a single state, but you know how it is, "Its working move on to the next phase" pressure.Still I am only using 20 % of the mega's program space. Do you have any simple FSM code that you are willing to share. I found the example for the remote car starter and learned a lot from that. Bad habits included. I would be done by now but I had to upgrade the motor controls from relays to an H-Bridge.


OK, I am looking at using a function to spool up and spool down the motors and handle the duration timer. Here is what I have that does not work. Please Help!
Code: [Select]

#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() {

  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   


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 = ");
  //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();
    Serial.print("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.print("motorspeed = MIN ");
  digitalWrite(23, HIGH);// Set Brake stir Motor
   Serial.print("Brake On ");



Here is what I have that does not work.

Does it do nothing, or not just not what you want ?

Every time the stir() function is called the stir speed will return to zero then ramp up.  Is that what you want ?  Do you want the program to stay in the stir() function for 10 seconds or for it to check each time that it is called whether it should ramp up the speed or whether it is time to ramp down the stir speed if it is running at full speed and has been for 10 seconds ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


What does it do? What would you like it to do? What serial output do you get?

Since you're calling it in loop, I assume you're expecting it to spool up for a 10 second stir and then wind down and stop and then do it all again repeatedly. If that's the case but it only works once, the likely cause is that you never set you second variable back to zero. After the wind down would be a good place.

Go Up