// https://wokwi.com/projects/410780548329232385
// for https://forum.arduino.cc/t/dc-motor-direction-speed-control-with-joystick/1307821
// and https://www.youtube.com/watch?v=wSjZIHMDczk
// Hookups modified from
// https://wokwi.com/projects/410601389043609601
// dc motor sim with encoders (taps on stepper quadrature signals)
// built from:
// https://wokwi.com/projects/410302035690579969
// ESC steppers simulating DC motor with L298N
//
// This project has two custom chips:
//
// L298N motor driver translates digital pins+PWM into
// PWM +/- signals for a DC motor./
//
// A custom chip to tranlate the PWM signals into quadrature
// signals for a stepper motor. Essentially a Stepper ESC.
//
// L298N Wokwi custom chip with LEDs-simulating motors
// and custom ESCs for PWM->Stepper control
// With potentiometers to control directions
// Also with @Dlloyd's Chip Scopes from https://github.com/Dlloydev/Wokwi-Chip-Scope
// to see the inputs abd outputs of the driver board
//
// Modified from
// https://wokwi.com/projects/351764383409373773 by Carlos Arino
int speedP = 5;
int dir1 = 4;
int dir2 = 3;
int mSpeed;
int rSpeed;
int xP = A0;
int xVal;
void setup() {
// put your setup code here, to run once:
pinMode(speedP, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(dir2, OUTPUT);
pinMode(xP, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
xVal = analogRead(xP);
mSpeed = map(xVal, 0, 1023, -255, 255);
if (mSpeed<-255){
mSpeed = -255;
}
if (mSpeed>255){
mSpeed = 255;
}
rSpeed = abs(mSpeed);
if (mSpeed<0){
digitalWrite(dir1, LOW);
digitalWrite(dir2, HIGH);
}
if (mSpeed>=0) {
digitalWrite(dir1, HIGH);
digitalWrite(dir2, LOW);
}
analogWrite(speedP, rSpeed);
Serial.print("x: ");
Serial.print(xVal);
Serial.print(", in range: ");
Serial.print(mSpeed);
Serial.print(", Abs val: ");
Serial.println(rSpeed);
}