/*
https://wokwi.com/projects/411094383161553921
Using https://github.com/drf5n/Wokwi-Chip-stepper-esc
Built from
https://wokwi.com/projects/410499111488041985
Demo of a simulated DC motor using a custom chip as an ESC
to translate analog PWM into stepper steps.
Encoder.h works on two quadrature signals like an encoder.
https://discord.com/channels/787627282663211009/887330958875967520/1290688230223642646
*/
/*
Uno with Scope https://github.com/Dlloydev/Wokwi-Chip-Scope
and stepper-esc https://github.com/drf5n/Wokwi-Chip-stepper-esc
Wokwi Uno https://wokwi.com/projects/390819301187622913
Wokwi Mega: https://wokwi.com/projects/390819455604080641
Wokwi ESP32S3 https://wokwi.com/projects/404720144387009537
See also https://wokwi.com/projects/359331973918199809
*/
#include <Encoder.h>
const byte INA = 10;
const byte INB = 9;
const byte trigPin = 3;
const byte PotPin = A0;
Encoder encoder(2,4);
void setup() {
Serial.begin(115200);
pinMode(INA, OUTPUT);
pinMode(INB, OUTPUT);
pinMode(trigPin, OUTPUT);
analogWrite(trigPin,127);
}
void loop() {
static int lastPot = -1;
int pot = (analogRead(PotPin)-512)/2;
if (pot != lastPot) {
Serial.print("pot:");
Serial.println(pot);
lastPot = pot;
if (pot >= 0) {
analogWrite(INA, pot);
analogWrite(INB, 0);
} else {
analogWrite(INA, 0);
analogWrite(INB, -pot-1); // off by one fix
}
}
encoder.read();
report();
}
void report(){
auto now = millis();
const typeof(now) interval = 1000;
static typeof(now) last = -interval;
if(now - last >= interval){
last+= interval;
long enc = encoder.read();
Serial.print("encoder:");
Serial.print(enc);
Serial.println();
}
}Wokwi UnoScope
https://wokwi.com/projects/390819301187622913
cnt/meas
en/!dis