1

Everything in this code seems to work as expected except the subroutine speedcw at the bottom. It should change the time_delay variable.

#define ENA 2 //pin 2 on chip
#define in_A 0 //pin 0 on chip IN2
#define in_B 1 //pin 1 on chip IN1
#define in_C 3 //pin 3 on chip AUX
#define in_D 4 //pin 4 on chip AUX
byte PWM_PIN = 5; //pin 5 on chip
int pwm_value = 1500;
int time_delay = 7;
void setup() {
 pinMode(ENA, OUTPUT);
 pinMode(in_A, OUTPUT);
 pinMode(in_B, OUTPUT);
 pinMode(in_C, OUTPUT);
 pinMode(in_D, OUTPUT);
 pinMode(PWM_PIN, INPUT);
}
void step1(){
 //A+,B+
 digitalWrite(in_A, 1);
 digitalWrite(in_B, 1);
 delay(time_delay);
}
void step2(){
 //A+,B-
 digitalWrite(in_A, 1);
 digitalWrite(in_B, 0);
 delay(time_delay);
}
void step3(){
 //A-,B-
 digitalWrite(in_A, 0);
 digitalWrite(in_B, 0);
 delay(time_delay);
}
void step4(){
 //A-,B+
 digitalWrite(in_A, 0);
 digitalWrite(in_B, 1);
 delay(time_delay);
}
void clockwise(long st){
 long i = 0;
 while( i < st ){
 //step1
 step1();
 //step2
 step2();
 //step3
 step3();
 //step4
 step4();
 i++;
 }
}
void counter_clockwise(long st){
 long i = 0;
 while( i < st ){
 //step1
 step4();
 //step4
 step3();
 //step3
 step2();
 //step2
 step1();
 i++;
 }
}
void loop() {
 pwm_value = pulseIn(PWM_PIN, HIGH);
 time_delay = 7;
 while (pwm_value <= 1450) {//red LED
 pwm_value = pulseIn(PWM_PIN, HIGH);
 digitalWrite(ENA, 1);
 // digitalWrite(in_C, 1); //red LED
 // digitalWrite(in_D, 0); //green LED
 // time_delay = 1;
 speedcw();
 clockwise(2);
 }
 while (pwm_value >= 1550) {//green LED
 pwm_value = pulseIn(PWM_PIN, HIGH);
 digitalWrite(ENA, 1);
 // digitalWrite(in_C, 0); //red LED
 // digitalWrite(in_D, 1); //green LED
 time_delay = 1;
 // speedccw();
 counter_clockwise(2);
 }
 while ((pwm_value >= 1451)&&(pwm_value <= 1549)) {
 pwm_value = pulseIn(PWM_PIN, HIGH);
 digitalWrite(ENA, 0);
 digitalWrite(in_C, 0);
 digitalWrite(in_D, 0);
 }
}
void speedcw(){
 if (pwm_value <= 900){
 time_delay = 1;
 }
 else if (pwm_value <= 1000){
 time_delay = 2;
 }
 else if (pwm_value <= 1100){
 time_delay = 3;
 }
 else if (pwm_value <= 1200){
 time_delay = 4;
 }
 else if (pwm_value <= 1300){
 time_delay = 5;
 }
 else if (pwm_value <= 1400){
 time_delay = 6;
 }
}
void speedccw(){
}
Edgar Bonet
45.1k4 gold badges42 silver badges81 bronze badges
asked Mar 6, 2023 at 20:58
3
  • 3
    Are you sure that 'pwm_value <= 1450' is ever true? If not, then the code would just skip that while loop and not even call speedcw(). Commented Mar 6, 2023 at 21:23
  • 1
    thank you, you were correct i made and error with the pwm i was feeding my code, 1300 was the lowest received. im new to the arduino. Commented Mar 7, 2023 at 14:43
  • 1
    @bigjosh - you should post that as an answer Commented Mar 5 at 0:22

1 Answer 1

1

If ‘pwm_value’ gets to 1401 or higher and stays there, ‘time_delay’ never changes from its 7 value.

For broad applicability, I would remove the assigning of 7 to ‘pwm_value’ in ‘void loop()’ and let the ‘case else’ code makes a substantive change and sustains it until driven by a new ‘pwm_value.’ Put an ‘else’ value in for condition where ‘pwm_value’ is 1401 or more (like 7, if that is what you intend.)

answered Mar 4 at 23:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.