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(){
}
2
  • 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().
    – bigjosh
    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. Mar 7, 2023 at 14:43

0

Your Answer

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

Browse other questions tagged or ask your own question.