0

I was trying to program my Arduino (along with a connected motor controller) to switch from cycle to cycle with this program. It doesn't seem to work however. There's still plenty of storage space and dynamic memory, so those aren't the issue. This is the program:

int EnablePin1 = 8;
int duty;
int period;
int cycle;

int PWMPinA1 = 11;  
int PWMPinB1 = 3;
int PWMPinA3 = 6;  
int PWMPinB3 = 5;

const byte CPin1 = A0;  

int CRaw1;      
void setup() {

    Serial.begin(9600);

    pinMode(EnablePin1, OUTPUT);

    pinMode(PWMPinA1, OUTPUT);
    pinMode(PWMPinB1, OUTPUT);
    pinMode(PWMPinA3, OUTPUT);
    pinMode(PWMPinB3, OUTPUT);

}

void loop() {
    digitalWrite(EnablePin1, HIGH); //enable the board
    for (duty = 0; duty <= 255; duty += 15) 
    {

        analogWrite(PWMPinB1, 0); //Set pinB to 0, when speed is written to pinA             

        analogWrite(PWMPinA1, 255);
        delay(850);
        if (PWMPinA1 == 11)
        {
            PWMPinA1 = 3;
            PWMPinB1 = 11;
        }
        else
        {
            PWMPinA1 = 11;
            PWMPinB1 = 3;
        }
   }
   analogWrite(PWMPinA1,0);//end at max speed
   analogWrite(PWMPinB1,0);
   CRaw1 = analogRead(CPin1);

   Serial.println("Feedback");

   Serial.print(CRaw1);


   delay(2000);
   //analogWrite(PWMPinB3, 0);
   for (duty = 255; duty >= 0; duty -= 51) // ramp down speed
   {
       //analogWrite(PWMPinA1, duty);
       analogWrite(PWMPinB3, 0);
       analogWrite(PWMPinA3, 255);
       delay(period);
       //delay(cycle);
       if(PWMPinA3 == 6)
       {
           PWMPinA3 = 5;
           PWMPinB3 = 6;
           //period = 850;
       }
       else
       {
           PWMPinA3 = 6;
           PWMPinB3 = 5;
          //period = 2500;
       }
   }
   analogWrite(PWMPinA3, 0); //set to 0 speed
   analogWrite(PWMPinB3, 0);
   delay(2000);

   digitalWrite(EnablePin1, LOW);// Toggle enable to reset the power chips           if we have had an overcurrent or overtemp fault

   for (cycle = 1000; cycle < 9000; cycle += 4000) 
   {
       //cycle=period;
       period=cycle;
   }

 /*for (cycle = 2000; cycle <= 3000; cycle += 1000) 
   {
       //cycle=period;
       period=10000;
   }*/
   for (cycle = 9000; cycle >= 1000; cycle -= 8000) 
   {
       period=0;
   }

   //delay(2000);  // Swap pins to make the motor change direction

}//end main loop

I've also tried having the cycle loops be this:

 for (cycle = 0; cycle < 9000; cycle += 3000) 
 {
     //cycle=period;
     period=cycle;
 }
 for (cycle = 9000; cycle >= 0; cycle -= 9000) 
 {
     //cycle=period/3;
     period=cycle;
 }

But that didn't do anything (I can see what PWM pins are powered through LEDs, and the first attempt, the LEDs only lit up for 1 second and the 2nd, they didn't light up at all.

3
  • 2
    doesn't seem to work .... this is a very poor description of observed results .... it contains no information that could be used to solve the problem ............ what do you expect to happen? .... what did happen? .... and what is your question? – jsotola May 21 '19 at 21:53
  • 1
    Why are you changing the values of the pin numbers? Doesn't seem good. And what should the for loops actually do? Currently they only set the variable period multiple times without doing something with it. The second loop only runs once. The variable period is not used ever again. This code part does absolutely nothing. You need to explain way more, what you want to achieve and what the problems are with this. – chrisl May 22 '19 at 7:58
  • 1
    duty should be renamed: it's not a duty cycle, and its name is misleading. period should be initialized before use. The loops over cycle should be removed: they serve no useful purpose (and the compiler will remove them anyway). The biggest issue is that, on reading the program, its purpose is not clear at all. And the description in your question (“switch from cycle to cycle”) makes no sense. If you want to have any hope of receiving a useful answer, you should write a clear and complete description of what the program is supposed to do. – Edgar Bonet May 22 '19 at 8:00
1

First things, try to use multiple function and call it from the loop()

You can use some code magic for dealing with multitasking on Arduino. It's a bit tricky to understand but it's working.

Here is an example :


const int BROCHE_LED_1 = 13;
const int BROCHE_LED_2 = 12;
const int BROCHE_LED_3 = 11;

const unsigned long BLINK_INTERVAL_1 = 1000;
const unsigned long BLINK_INTERVAL_2 = 500;
const unsigned long BLINK_INTERVAL_3 = 2000;

void setup() {

  pinMode(BROCHE_LED_1, OUTPUT);
  pinMode(BROCHE_LED_2, OUTPUT);
  pinMode(BROCHE_LED_3, OUTPUT);

  digitalWrite(BROCHE_LED_1, LOW);
  digitalWrite(BROCHE_LED_2, LOW);
  digitalWrite(BROCHE_LED_3, LOW);
}

void loop() {

  task_led1();
  task_led2();
  task_led3();
}

void task_led1() {
  static unsigned long previousMillisLed1 = 0;
  static byte etatBrocheLed1 = LOW;

  unsigned long currentMillis = millis();


  if(currentMillis - previousMillisLed1 >= BLINK_INTERVAL_1) {

    previousMillisLed1 = currentMillis;

    etatBrocheLed1 = !etatBrocheLed1;
    digitalWrite(BROCHE_LED_1, etatBrocheLed1);
  }
}

void task_led2() {
  static unsigned long previousMillisLed2 = 0;
  static byte etatBrocheLed2 = LOW;

  unsigned long currentMillis = millis();


  if(currentMillis - previousMillisLed2 >= BLINK_INTERVAL_2) {

    previousMillisLed2 = currentMillis;

    etatBrocheLed2 = !etatBrocheLed2;
    digitalWrite(BROCHE_LED_2, etatBrocheLed2);
  }
}

void task_led3() {
  static unsigned long previousMillisLed3 = 0;
  static byte etatBrocheLed3 = LOW;

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillisLed3 >= BLINK_INTERVAL_3) {

    previousMillisLed3 = currentMillis;

    etatBrocheLed3 = !etatBrocheLed3;
    digitalWrite(BROCHE_LED_3, etatBrocheLed3);
  }
}

Second things, Try to use Real time operating system

You can try to use FreeRTOS, it's work with most arduino and you got lot of infos on the web.

It make multi tasking way more easy than the previous example.

Here is the Docs i've found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.