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(){
}
-
3Are 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– bigjosh03/06/2023 21:23:26Commented Mar 6, 2023 at 21:23
-
1thank 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.benjamin nikkel– benjamin nikkel03/07/2023 14:43:30Commented Mar 7, 2023 at 14:43
-
1@bigjosh - you should post that as an answerGreenonline– Greenonline03/05/2025 00:22:42Commented Mar 5 at 0:22
1 Answer 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.)