Noobie here. I search several forums and videos but can't seem to find any answers. What I'm trying to do is: Modify the below code to move the motor clockwise and counterclockwise based on the press of two buttons. The motor should turn continuously until the direction is changed again.
Here is the code I need to modify:
#include <util/atomic.h> // For the ATOMIC_BLOCK macro
#define ENCA 2 // YELLOW
#define ENCB 3 // WHITE
#define PWM 5
#define IN2 7
#define IN1 8
#define clk 9
#define counterclk 10
int dir, pwmVal, pwm, pwr;
volatile int posi = 0; // specify posi as volatile: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/
void setup() {
Serial.begin(9600);
pinMode(ENCA,INPUT);
pinMode(ENCB,INPUT);
attachInterrupt(digitalPinToInterrupt(ENCA),readEncoder,RISING);
pinMode(PWM,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
Serial.println("Task2 L298N control DC motor");
}
void loop() {
// Read the position in an atomic block to avoid a potential
// misread if the interrupt coincides with this code running
// see: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/
int pos = 0;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
pos = posi;
}
pwmVal = 120; // sample pwm value to move the motor
/*
* Probably you want add a button reading here and update
* pwm value
*/
if(pwmVal>0) {
dir = 1;
}
else if (pwmVal <0) {
dir = -1;
}
pwr =fabs(pwmVal);
// signal the motor
setMotor(dir,pwr,PWM,IN1,IN2);
Serial.print("PWM value: ");
Serial.print(pwr);
Serial.print(" | Directon: ");
Serial.print(dir);
Serial.println();
}
/*
* Probably your function to read buttons and assign pwm values
*/
void setMotor(int dir, int pwmVal, int pwm, int in1, int in2){
analogWrite(pwm,pwmVal);
if(dir == 1){
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
}
else if(dir == -1){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
else{
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
}
}
void readEncoder(){
int b = digitalRead(ENCB);
if(b > 0){
posi++;
}
else{
posi--;
}
}
Below is my Circuit Diagram:
![F|690x422](upload://zyIcJyVfz9AQvB8kfCrnnRTowbQ.jpeg)
Any help would be gereatly appreciated!
Sorry but that's a coloured bird nest lacking most of value. Bread bord surface as well as the blue board of an UNO tells nothing.
That PP3 battery tells a lot. Get rid of it! It delivers 9 volt but not any Amps.
is not a schematic. It's a Fritzing toy picture not showing things the way any engineer would use them. A desperate criminal detective solving a tricky murder would create a proper drawing of that mess. Helpers don't spend time on that.
Maybe not. Once one of the switches is made and the logic responds the output won't be bouncing even though the switch might. It's basically a latch circuit. Now, if malignant operators are involved that changes things.
Sorry, I'm oboviously a dunce. I did your recommendations above but nothing is happening. After rereading the instructions above, I need to use the "pwmVal" variable and can only update or just add to the existing code.
pwmVal = 120; // sample pwm value to move the motor
/*
** Probably you want add a button reading here and update *
* * pwm value*
*/
if(pwmVal>0) {
dir = 1;
Well, I've been working on this problem for abopput 2-1/2 weeks now (on and off). At the beginning, I wasn't looking for you guys to simply give me the correct code because I want to learn this but this assignment is due in the next few days. Would it be to much of a problem for you show me the code if you had this assignment? My anxiety is starting to kick in. I'll understand if this isn't meeting the forum policy.
Thank you
No, no, no...I"m not asking for you to give the code. My appolgies if I'm coming across that way. I'm just looking for guidence if you were to complete this assignment. I would rather fail the assignment but learn little than pass and learn nothing.
It sounds like you asking how to modify the pwmVal variable based on button presses, and let the rest of the code use the pwmVal value to run the motor. Is that it?