From d512a56b03d981bae82f32f93647f686162f117f Mon Sep 17 00:00:00 2001 From: mattmcw Date: Thu, 11 Jan 2024 23:14:02 -0500 Subject: [PATCH] Arduino sketches, proofs of concept for different features --- ino/contact_printer/contact_printer.ino | 1 - ino/esp32_dc_motor/esp32_dc_motor.ino | 73 +++++++++++++++++++++ ino/motor_encoder_poc/motor_encoder_poc.ino | 56 ++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 ino/esp32_dc_motor/esp32_dc_motor.ino create mode 100644 ino/motor_encoder_poc/motor_encoder_poc.ino diff --git a/ino/contact_printer/contact_printer.ino b/ino/contact_printer/contact_printer.ino index a7249b3..2f495ab 100644 --- a/ino/contact_printer/contact_printer.ino +++ b/ino/contact_printer/contact_printer.ino @@ -1,5 +1,4 @@ #include "ContactPrinter.h"; - void setup () {} void loop () {} \ No newline at end of file diff --git a/ino/esp32_dc_motor/esp32_dc_motor.ino b/ino/esp32_dc_motor/esp32_dc_motor.ino new file mode 100644 index 0000000..3279f70 --- /dev/null +++ b/ino/esp32_dc_motor/esp32_dc_motor.ino @@ -0,0 +1,73 @@ +//https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/ESP32/ESP32_DC_Motor.ino + +/********* + Rui Santos + Complete project details at http://randomnerdtutorials.com +*********/ + +// Motor A +int motor1Pin1 = 27; +int motor1Pin2 = 26; +int enable1Pin = 14; + +// Setting PWM properties +const int freq = 30000; +const int pwmChannel = 0; +const int resolution = 8; +int dutyCycle = 200; + +void setup() { + // sets the pins as outputs: + pinMode(motor1Pin1, OUTPUT); + pinMode(motor1Pin2, OUTPUT); + pinMode(enable1Pin, OUTPUT); + + // configure LED PWM functionalitites + ledcSetup(pwmChannel, freq, resolution); + + // attach the channel to the GPIO to be controlled + ledcAttachPin(enable1Pin, pwmChannel); + + Serial.begin(115200); + + // testing + Serial.print("Testing DC Motor..."); +} + +void loop() { + // Move the DC motor forward at maximum speed + Serial.println("Moving Forward"); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, HIGH); + delay(2000); + + // Stop the DC motor + Serial.println("Motor stopped"); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + delay(1000); + + // Move DC motor backwards at maximum speed + Serial.println("Moving Backwards"); + digitalWrite(motor1Pin1, HIGH); + digitalWrite(motor1Pin2, LOW); + delay(2000); + + // Stop the DC motor + Serial.println("Motor stopped"); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + delay(1000); + + // Move DC motor forward with increasing speed + digitalWrite(motor1Pin1, HIGH); + digitalWrite(motor1Pin2, LOW); + while (dutyCycle <= 255){ + ledcWrite(pwmChannel, dutyCycle); + Serial.print("Forward with duty cycle: "); + Serial.println(dutyCycle); + dutyCycle = dutyCycle + 5; + delay(500); + } + dutyCycle = 200; +} diff --git a/ino/motor_encoder_poc/motor_encoder_poc.ino b/ino/motor_encoder_poc/motor_encoder_poc.ino new file mode 100644 index 0000000..65a30c8 --- /dev/null +++ b/ino/motor_encoder_poc/motor_encoder_poc.ino @@ -0,0 +1,56 @@ +/*** + * + * 100RPM 12VDC Worm Gear Motor w/ encoder + * SKU - JGY-370 + * DC12V100RPM - SKU-GS00127-05 + * + * Gear ratio: 40:1 + * + * Red——Motor power + (exchange can control rotating and reversing) + * Black——Coding power- negative (3.3-5V) polarity cannot be wrong + * Yellow——Signal feedback + * Green——Signal feedback + * Blue——Coding power + positive(3.3-5V)polarity cannot be wrong + * White——Motor power - (exchange can control rotating and + * + ***/ + + + +#include // For the ATOMIC_BLOCK macro + +#define ENCA 2 // YELLOW +#define ENCB 3 // WHITE +#define MOTORA 10 +#define MOTORB 11 + +volatile int posi = 0; // specify posi as volatile + +void setup() { + Serial.begin(9600); + pinMode(ENCA, INPUT); + pinMode(ENCB, INPUT); + attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder,RISING); +} + +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; + } + + Serial.println(pos); +} + +void readEncoder(){ + int b = digitalRead(ENCB); + if(b > 0){ + posi++; + } + else{ + posi--; + } +}