0

working on a project to make a rover-like vehicle move, and was running into a problem where our stepper DC motors are overheating. I suspect that it's because of the multiple "if" branches we're running through a loop in our code. Was wondering if anyone had any ideas to make this more efficient?

void loop() {         
cstore=pulseIn(A3, HIGH);         //values for our radio controller to communicate with board (Arduino UNO)
lstore=pulseIn(A2, HIGH);
rstore=pulseIn(A0, HIGH);

rmove = map(rstore, 994,1872, -255, 255); //center over zero
//rmove=map(rmovepre, -255,255, -500, 500);

lmove = map(lstore, 1007,1989, -255, 255); //center over zero
lmoveinv=-1*lmove;
rmoveinv=-1*rmove;



//Serial.println("CH1   CH2   CH4");
//Serial.print(cstore);
Serial.print("   ");
Serial.print(lmoveinv);
Serial.print("   ");
Serial.print(rmove);
Serial.print("   ");
Serial.println(cstore);



//valid pwm values

/*What we're doing here is determining whether we want to move
forward or backward*/
if(rmove>0) {
  Motor1_Forward(rmove); // back right
  Motor2_Forward(rmove); // front right
}

if(lmoveinv>30) {
  Motor3_Forward(lmoveinv);
  Motor4_Forward(lmoveinv);
}

if(rmove<-70) {
  Motor1_Backward(rmoveinv);
  Motor2_Backward(rmoveinv);
}

if(lmoveinv<-30) {
  Motor3_Backward(lmove);
  Motor4_Backward(lmove);
}

if(cstore<1200) {
  Motor1_Brake();
  Motor2_Brake();
  Motor3_Brake();
  Motor4_Brake();
}
3
  • If statements are efficient, but are you sure your if logic is correct? Currently, if any statement evaluates to true, it will occur regardless of other statements. Typically, we always use if() {} else if() {} else {} for that type of structure. Could cstore<1200 evaluate to true while any other condition is also true? This would cause breaking while moving forwards, perhaps? – Denis G. Labrecque Dec 7 '20 at 0:29
  • reduce your code to move one motor in one direction, nothing else ... no if statements, no serial.print – jsotola Dec 7 '20 at 1:00
  • How do you think heats the structure of your code the stepper motor? Do you mean the motor overheats because you overload it, for example by constantly let it move forth and back? Then the solution is clear: don't do this. – the busybee Dec 7 '20 at 8:04

Your Answer

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

Browse other questions tagged or ask your own question.