Skip to main content
removed a irrelevant section, which was caused by confusion in response to an incorrect answer (my b).
Source Link
  1. I put a while loop on the limitswitch. I want that when limitswitch is hit the motor stops, then reverse the direction and disengage the limitswitch moving for the disengage_ls time.

  2. I have some doubts on the comment on zero subtraction on the following part:

    I put a while loop on the limitswitch. I want that when limitswitch is hit the motor stops, then reverse the direction and disengage the limitswitch moving for the disengage_ls time.
 if (upState && !downState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } 

I'm setting the time to zero after each time I increase the speed, in order to reset the counter for the next speed increase (where accel_interval is the time between 2 different speed set. I don't understand why it was suggested to remove it

  1. I put a while loop on the limitswitch. I want that when limitswitch is hit the motor stops, then reverse the direction and disengage the limitswitch moving for the disengage_ls time.

  2. I have some doubts on the comment on zero subtraction on the following part:

 if (upState && !downState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } 

I'm setting the time to zero after each time I increase the speed, in order to reset the counter for the next speed increase (where accel_interval is the time between 2 different speed set. I don't understand why it was suggested to remove it

  1. I put a while loop on the limitswitch. I want that when limitswitch is hit the motor stops, then reverse the direction and disengage the limitswitch moving for the disengage_ls time.
updated based on comments
Source Link
Luigi
  • 181
  • 7

Any insights would be greatly appreciated! ###update: I've updated the code trying to follow suggestions. Please let me know if I understood correctly.

  1. I put a while loop on the limitswitch. I want that when limitswitch is hit the motor stops, then reverse the direction and disengage the limitswitch moving for the disengage_ls time.

  2. I have some doubts on the comment on zero subtraction on the following part:

 if (upState && !downState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } 

I'm setting the time to zero after each time I increase the speed, in order to reset the counter for the next speed increase (where accel_interval is the time between 2 different speed set. I don't understand why it was suggested to remove it

//Head motor int in1Head = 44; //on ARDUINO MEGA int in2Head = 45; int limSwHead = 19; //2 normally close limit switches connected in series int FAST = 16; //push button int UP = 17; //push button int DOWN = 15; //push button volatile bool limSwHeadTriggered = false; // Direction enum and flag enum Direction { FORWARD, BACKWARD }; Direction currentDirection = FORWARD; // Initialize with default direction uint8_t PWM_fast =70; //value in % of motor speed uint8_t PWM_slow =30; //value in % of motor speed uint8_t PWM = 20; //min value of PWM (to be changed also below) unsigned long zero = 0; uint16_t accel_interval = 10;//time in millis to change the speed in acceleration uint8_t speedIncrease = 5; //% of speed increase for each accel_interval uint16_t disengage_ls = 500; //time in millis to disangage Limitswitch when hit boolean fastState = false; boolean upState = false; boolean downState = false; //----FUNCTIONS---------------------------------------------------------------------------------------- //INTERRUPTS void headISR() { limSwHeadTriggered = true; } void HeadForward(uint8_t PWM) { analogWrite(in1Head, 255); analogWrite(in2Head,(255 * (100 - PWM)) / 100); //inverse of the PWM in range 0-255 currentDirection = FORWARD; // Set direction flag } void HeadBackward(uint8_t PWM) { analogWrite(in1Head,(255 * (100 - PWM))/ 100); //inverse of the PWM in range 0-255 analogWrite(in2Head, 255); currentDirection = BACKWARD; // Set direction flag } void HeadStop() { analogWrite(in1Head,255); analogWrite(in2Head, 255); PWM=0;PWM=20; } void HeadCoast() { analogWrite(in1Head,0); analogWrite(in2Head, 0); PWM=0;PWM=20; } unsigned longvoid limSwHit() { HeadStop(); unsigned long initialMillis = millis(); if(currentDirection == FORWARD){ HeadBackward(PWM_slow); } else if (currentDirection == BACKWARD){ HeadForward(PWM_slow); }  return initialMillis; } void setup() { Serial.begin(9600); //head motor pinMode(in1Head, OUTPUT); pinMode(in2Head, OUTPUT); //limitswitch pinMode(limSwHead, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(limSwHead), headISR, RISING); // Trigger on HIGH signal //Buttons pinMode(FAST,INPUT_PULLUP); pinMode(UP,INPUT_PULLUP); pinMode(DOWN,INPUT_PULLUP); } void loop() { if(limSwHeadTriggered){ unsigned long initialMillis = limSwHitmillis(); ifwhile(millis()- initialMillis>=initialMillis<= disengage_ls){ HeadStoplimSwHit(); limSwHeadTriggered=digitalRead(limSwHead);} Serial.printlnHeadCoast("limitswitch"); } limSwHeadTriggered=digitalRead(limSwHead); } // read the state of the pushbutton value, inverted due to pullup resistors: upState = !digitalRead(UP); downState = !digitalRead(DOWN); fastState = !digitalRead(FAST); if (upState && !downState && !fastState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } else if(!upState && downState && (millis()- zero >= accel_interval)) { if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadBackward(PWM); Serial.println("moving down:"); zero = millis(); } else{ HeadCoast(); } } 

Any insights would be greatly appreciated!

//Head motor int in1Head = 44; //on ARDUINO MEGA int in2Head = 45; int limSwHead = 19; //2 normally close limit switches connected in series int FAST = 16; //push button int UP = 17; //push button int DOWN = 15; //push button volatile bool limSwHeadTriggered = false; // Direction enum and flag enum Direction { FORWARD, BACKWARD }; Direction currentDirection = FORWARD; // Initialize with default direction uint8_t PWM_fast =70; //value in % of motor speed uint8_t PWM_slow =30; //value in % of motor speed uint8_t PWM = 20; unsigned long zero = 0; uint16_t accel_interval = 10;//time in millis to change the speed in acceleration uint8_t speedIncrease = 5; //% of speed increase for each accel_interval uint16_t disengage_ls = 500; //time in millis to disangage Limitswitch when hit boolean fastState = false; boolean upState = false; boolean downState = false; //----FUNCTIONS---------------------------------------------------------------------------------------- //INTERRUPTS void headISR() { limSwHeadTriggered = true; } void HeadForward(uint8_t PWM) { analogWrite(in1Head, 255); analogWrite(in2Head,(255 * (100 - PWM)) / 100); //inverse of the PWM in range 0-255 currentDirection = FORWARD; // Set direction flag } void HeadBackward(uint8_t PWM) { analogWrite(in1Head,(255 * (100 - PWM))/ 100); //inverse of the PWM in range 0-255 analogWrite(in2Head, 255); currentDirection = BACKWARD; // Set direction flag } void HeadStop() { analogWrite(in1Head,255); analogWrite(in2Head, 255); PWM=0; } void HeadCoast() { analogWrite(in1Head,0); analogWrite(in2Head, 0); PWM=0; } unsigned long limSwHit() { HeadStop(); unsigned long initialMillis = millis(); if(currentDirection == FORWARD){ HeadBackward(PWM_slow); } else if (currentDirection == BACKWARD){ HeadForward(PWM_slow); }  return initialMillis; } void setup() { Serial.begin(9600); //head motor pinMode(in1Head, OUTPUT); pinMode(in2Head, OUTPUT); //limitswitch pinMode(limSwHead, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(limSwHead), headISR, RISING); // Trigger on HIGH signal //Buttons pinMode(FAST,INPUT_PULLUP); pinMode(UP,INPUT_PULLUP); pinMode(DOWN,INPUT_PULLUP); } void loop() { if(limSwHeadTriggered){ unsigned long initialMillis = limSwHit(); if(millis()- initialMillis>= disengage_ls){ HeadStop(); limSwHeadTriggered=digitalRead(limSwHead); Serial.println("limitswitch"); } } // read the state of the pushbutton value, inverted due to pullup resistors: upState = !digitalRead(UP); downState = !digitalRead(DOWN); fastState = !digitalRead(FAST); if (upState && !downState && !fastState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } else if(!upState && downState && (millis()- zero >= accel_interval)) { if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadBackward(PWM); Serial.println("moving down:"); zero = millis(); } else{ HeadCoast(); } } 

Any insights would be greatly appreciated! ###update: I've updated the code trying to follow suggestions. Please let me know if I understood correctly.

  1. I put a while loop on the limitswitch. I want that when limitswitch is hit the motor stops, then reverse the direction and disengage the limitswitch moving for the disengage_ls time.

  2. I have some doubts on the comment on zero subtraction on the following part:

 if (upState && !downState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } 

I'm setting the time to zero after each time I increase the speed, in order to reset the counter for the next speed increase (where accel_interval is the time between 2 different speed set. I don't understand why it was suggested to remove it

//Head motor int in1Head = 44; //on ARDUINO MEGA int in2Head = 45; int limSwHead = 19; //2 normally close limit switches connected in series int FAST = 16; //push button int UP = 17; //push button int DOWN = 15; //push button volatile bool limSwHeadTriggered = false; // Direction enum and flag enum Direction { FORWARD, BACKWARD }; Direction currentDirection = FORWARD; // Initialize with default direction uint8_t PWM_fast =70; //value in % of motor speed uint8_t PWM_slow =30; //value in % of motor speed uint8_t PWM = 20; //min value of PWM (to be changed also below) unsigned long zero = 0; uint16_t accel_interval = 10;//time in millis to change the speed in acceleration uint8_t speedIncrease = 5; //% of speed increase for each accel_interval uint16_t disengage_ls = 500; //time in millis to disangage Limitswitch when hit boolean fastState = false; boolean upState = false; boolean downState = false; //----FUNCTIONS---------------------------------------------------------------------------------------- //INTERRUPTS void headISR() { limSwHeadTriggered = true; } void HeadForward(uint8_t PWM) { analogWrite(in1Head, 255); analogWrite(in2Head,(255 * (100 - PWM)) / 100); //inverse of the PWM in range 0-255 currentDirection = FORWARD; // Set direction flag } void HeadBackward(uint8_t PWM) { analogWrite(in1Head,(255 * (100 - PWM))/ 100); //inverse of the PWM in range 0-255 analogWrite(in2Head, 255); currentDirection = BACKWARD; // Set direction flag } void HeadStop() { analogWrite(in1Head,255); analogWrite(in2Head, 255); PWM=20; } void HeadCoast() { analogWrite(in1Head,0); analogWrite(in2Head, 0); PWM=20; } void limSwHit() { HeadStop(); if(currentDirection == FORWARD){ HeadBackward(PWM_slow); } else if (currentDirection == BACKWARD){ HeadForward(PWM_slow); } } void setup() { Serial.begin(9600); //head motor pinMode(in1Head, OUTPUT); pinMode(in2Head, OUTPUT); //limitswitch pinMode(limSwHead, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(limSwHead), headISR, RISING); // Trigger on HIGH signal //Buttons pinMode(FAST,INPUT_PULLUP); pinMode(UP,INPUT_PULLUP); pinMode(DOWN,INPUT_PULLUP); } void loop() { if(limSwHeadTriggered){ unsigned long initialMillis = millis(); while(millis()- initialMillis<= disengage_ls){ limSwHit(); } HeadCoast()  limSwHeadTriggered=digitalRead(limSwHead); } // read the state of the pushbutton value, inverted due to pullup resistors: upState = !digitalRead(UP); downState = !digitalRead(DOWN); fastState = !digitalRead(FAST); if (upState && !downState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } else if(!upState && downState && (millis()- zero >= accel_interval)) { if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadBackward(PWM); Serial.println("moving down:"); zero = millis(); } else{ HeadCoast(); } } 
improved formatting
Source Link
Luigi
  • 181
  • 7
//Head motor int in1Head = 44; //on ARDUINO MEGA int in2Head = 45; int limSwHead = 19; //2 normally close limit switches connected in series int FAST = 16; //push button int UP = 17; //push button int DOWN = 15; //push button volatile bool limSwHeadTriggered = false; // Direction enum and flag enum Direction { FORWARD, BACKWARD }; Direction currentDirection = FORWARD; // Initialize with default direction uint8_t PWM_fast =70; //value in % of motor speed uint8_t PWM_slow =30; //value in % of motor speed uint8_t PWM = 20; unsigned long zero = 0; uint16_t accel_interval = 10;//time in millis to change the speed in acceleration uint8_t speedIncrease = 5; //% of speed increase for each accel_interval uint16_t disengage_ls = 500; //time in millis to disangage Limitswitch when hit boolean fastState = false; boolean upState = false; boolean downState = false; //----FUNCTIONS---------------------------------------------------------------------------------------- //INTERRUPTS void headISR() { limSwHeadTriggered = true; } void HeadForward(uint8_t PWM) { analogWrite(in1Head, 255); analogWrite(in2Head,(255 * (100 - PWM)) / 100); //inverse of the PWM in range 0-255 currentDirection = FORWARD; // Set direction flag } void HeadBackward(uint8_t PWM) { analogWrite(in1Head,(255 * (100 - PWM))/ 100); //inverse of the PWM in range 0-255 analogWrite(in2Head, 255); currentDirection = BACKWARD; // Set direction flag } void HeadStop() { analogWrite(in1Head,255); analogWrite(in2Head, 255); PWM=0; } void HeadCoast() { analogWrite(in1Head,0); analogWrite(in2Head, 0); PWM=0; } unsigned long limSwHit() { HeadStop(); unsigned long initialMillis = millis(); if(currentDirection == FORWARD){ HeadBackward(PWM_slow); } else if (currentDirection == BACKWARD){ HeadForward(PWM_slow); } return initialMillis; } void setup() { Serial.begin(9600); //head motor pinMode(in1Head, OUTPUT); pinMode(in2Head, OUTPUT); //limitswitch pinMode(limSwHead, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(limSwHead), headISR, RISING); // Trigger on HIGH signal //Buttons pinMode(FAST,INPUT_PULLUP); pinMode(UP,INPUT_PULLUP); pinMode(DOWN,INPUT_PULLUP); } void loop() { if(limSwHeadTriggered){ unsigned long initialMillis = limSwHit(); if(millis()- initialMillis>= disengage_ls){ HeadStop(); limSwHeadTriggered=digitalRead(limSwHead); Serial.println("limitswitch"); } } // read the state of the pushbutton value, inverted due to pullup resistors: upState = !digitalRead(UP); downState = !digitalRead(DOWN); fastState = !digitalRead(FAST); if (upState && !downState && !fastState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } else if(!upState && downState && (millis()- zero >= accel_interval)) { if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadBackward(PWM); Serial.println("moving down:"); zero = millis(); } else{ HeadCoast(); } } 
//Head motor int in1Head = 44; //on ARDUINO MEGA int in2Head = 45; int limSwHead = 19; //2 normally close limit switches connected in series int FAST = 16; //push button int UP = 17; //push button int DOWN = 15; //push button volatile bool limSwHeadTriggered = false; // Direction enum and flag enum Direction { FORWARD, BACKWARD }; Direction currentDirection = FORWARD; // Initialize with default direction uint8_t PWM_fast =70; //value in % of motor speed uint8_t PWM_slow =30; //value in % of motor speed uint8_t PWM = 20; unsigned long zero = 0; uint16_t accel_interval = 10;//time in millis to change the speed in acceleration uint8_t speedIncrease = 5; //% of speed increase for each accel_interval uint16_t disengage_ls = 500; //time in millis to disangage Limitswitch when hit boolean fastState = false; boolean upState = false; boolean downState = false; //----FUNCTIONS---------------------------------------------------------------------------------------- //INTERRUPTS void headISR() { limSwHeadTriggered = true; } void HeadForward(uint8_t PWM) { analogWrite(in1Head, 255); analogWrite(in2Head,(255 * (100 - PWM)) / 100); //inverse of the PWM in range 0-255 currentDirection = FORWARD; // Set direction flag } void HeadBackward(uint8_t PWM) { analogWrite(in1Head,(255 * (100 - PWM))/ 100); //inverse of the PWM in range 0-255 analogWrite(in2Head, 255); currentDirection = BACKWARD; // Set direction flag } void HeadStop() { analogWrite(in1Head,255); analogWrite(in2Head, 255); PWM=0; } void HeadCoast() { analogWrite(in1Head,0); analogWrite(in2Head, 0); PWM=0; } unsigned long limSwHit() { HeadStop(); unsigned long initialMillis = millis(); if(currentDirection == FORWARD){ HeadBackward(PWM_slow); } else if (currentDirection == BACKWARD){ HeadForward(PWM_slow); } return initialMillis; } void setup() { Serial.begin(9600); //head motor pinMode(in1Head, OUTPUT); pinMode(in2Head, OUTPUT); //limitswitch pinMode(limSwHead, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(limSwHead), headISR, RISING); // Trigger on HIGH signal //Buttons pinMode(FAST,INPUT_PULLUP); pinMode(UP,INPUT_PULLUP); pinMode(DOWN,INPUT_PULLUP); } void loop() { if(limSwHeadTriggered){ unsigned long initialMillis = limSwHit(); if(millis()- initialMillis>= disengage_ls){ HeadStop(); limSwHeadTriggered=digitalRead(limSwHead); Serial.println("limitswitch"); } } // read the state of the pushbutton value, inverted due to pullup resistors: upState = !digitalRead(UP); downState = !digitalRead(DOWN); fastState = !digitalRead(FAST); if (upState && !downState && !fastState && (millis()- zero >= accel_interval)){ if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadForward(PWM); Serial.println("moving up"); zero = millis(); } else if(!upState && downState && (millis()- zero >= accel_interval)) { if (PWM < (fastState ? PWM_fast : PWM_slow)) { PWM += speedIncrease; } HeadBackward(PWM); Serial.println("moving down:"); zero = millis(); } else{ HeadCoast(); } } 
Source Link
Luigi
  • 181
  • 7
Loading