From 8e22684baeb1cdfddfe508f1203ce0a40e6b2f74 Mon Sep 17 00:00:00 2001 From: Nikodem Date: Thu, 23 May 2019 21:14:44 +0200 Subject: [PATCH] First commit --- Encoded motor/knob.ino | 61 +++++++++++++++++++++++++++++++++ Encoded motor/serial.ino | 74 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 Encoded motor/knob.ino create mode 100644 Encoded motor/serial.ino diff --git a/Encoded motor/knob.ino b/Encoded motor/knob.ino new file mode 100644 index 0000000..d869ba7 --- /dev/null +++ b/Encoded motor/knob.ino @@ -0,0 +1,61 @@ +#include +volatile long int encoder_pos = 0; +PIDController pos_pid; +int motor_value = 255; + +void setup() { + + Serial.begin(9600); + pinMode(2, INPUT); + pinMode(3, INPUT); + pinMode(9, OUTPUT); + pinMode(10, OUTPUT); + attachInterrupt(digitalPinToInterrupt(2), encoder, RISING); + + pos_pid.begin(); + pos_pid.tune(20, 0, 200); + pos_pid.limit(-255, 255); +} + +void loop() { +pos_pid.setpoint((int)(((float)analogRead(A0)/1023) * 374.0 *1)); + motor_value = pos_pid.compute(encoder_pos); +if(motor_value > 0){ + MotorCounterClockwise(motor_value); +}else{ + MotorClockwise(abs(motor_value)); +} + Serial.println(encoder_pos); + delay(2); +} + + + +void encoder(){ + + if(digitalRead(3) == HIGH){ + encoder_pos++; + }else{ + encoder_pos--; + } +} + +void MotorClockwise(int power){ + if(power > 50){ + analogWrite(9, power); + digitalWrite(10, LOW); + }else{ + digitalWrite(9, LOW); + digitalWrite(10, LOW); + } +} + +void MotorCounterClockwise(int power){ + if(power > 50){ + analogWrite(10, power); + digitalWrite(9, LOW); + }else{ + digitalWrite(9, LOW); + digitalWrite(10, LOW); + } +} diff --git a/Encoded motor/serial.ino b/Encoded motor/serial.ino new file mode 100644 index 0000000..0beb991 --- /dev/null +++ b/Encoded motor/serial.ino @@ -0,0 +1,74 @@ +#include +volatile long int encoder_pos = 0; +PIDController pos_pid; +int motor_value = 255; +unsigned int integerValue=0; // Max value is 65535 +char incomingByte; +void setup() { + + Serial.begin(9600); + pinMode(2, INPUT); + pinMode(3, INPUT); + pinMode(9, OUTPUT); + pinMode(10, OUTPUT); + attachInterrupt(digitalPinToInterrupt(2), encoder, RISING); + + pos_pid.begin(); + pos_pid.tune(15, 0, 2000); + pos_pid.limit(-255, 255); +} + +void loop() { + +if (Serial.available() > 0) { + integerValue = 0; + while(1) { + incomingByte = Serial.read(); + if (incomingByte == '\n') break; + if (incomingByte == -1) continue; + integerValue *= 10; + integerValue = ((incomingByte - 48) + integerValue); + pos_pid.setpoint(integerValue); + } +} + + motor_value = pos_pid.compute(encoder_pos); +if(motor_value > 0){ + MotorCounterClockwise(motor_value); +}else{ + MotorClockwise(abs(motor_value)); +} + Serial.println(encoder_pos); + delay(10); +} + + + +void encoder(){ + + if(digitalRead(3) == HIGH){ + encoder_pos++; + }else{ + encoder_pos--; + } +} + +void MotorClockwise(int power){ + if(power > 100){ + analogWrite(9, power); + digitalWrite(10, LOW); + }else{ + digitalWrite(9, LOW); + digitalWrite(10, LOW); + } +} + +void MotorCounterClockwise(int power){ + if(power > 100){ + analogWrite(10, power); + digitalWrite(9, LOW); + }else{ + digitalWrite(9, LOW); + digitalWrite(10, LOW); + } +}