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.
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?for
loops actually do? Currently they only set the variableperiod
multiple times without doing something with it. The second loop only runs once. The variableperiod
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.duty
should be renamed: it's not a duty cycle, and its name is misleading.period
should be initialized before use. The loops overcycle
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.