Hi All,
I am currently working on a project But really struggling.
The project is to control the speed of a motor based on the RPM/pulses of a wheel. I have two separate proxy sensors - one for the wheel and one for the motor.
I have a 17 tooth gear connected to a wheel and i have a proxy sensor counting each time a tooth of the gear goes past. I have the same setup for the motor. I need the motor to run at half the speed that the wheel is spinning Using some kind of (Probably PID Loop).
I will put my current code bellow but right now it isnt a very gradual speed increase and decrease and it is also like a second delayed.
Thanks in Advance
#include <arduino-timer.h>
auto timer = timer_create_default();
bool docounting1 = true;
int pulses1 = 0;
int pwmvalue1;
bool docounting2 = true;
int pulses2 = 0;
int pwmvalue2;
int val1;
int val2;
int motorPin = 10;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
timer.every(500, countPulses);
}
void loop() {
timer.tick();
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
val1 = digitalRead(2);
val2 = digitalRead(4);
if (val1 == LOW && docounting1 == true) {
docounting1 = false;
pulses1 = pulses1 + 1;
} else if (val1 == HIGH) {
docounting1 = true;
}
if (val2 == LOW && docounting2 == true) {
docounting2 = false;
pulses2 = pulses2 + 1;
} else if (val2 == HIGH) {
docounting2 = true;
}
analogWrite(motorPin, pwmvalue1);
}
void countPulses() {
Serial.print("Pulses in half a second (Pulse 1): ");
Serial.println(pulses1);
pwmvalue1 = map(pulses1, 0, 10, 0, 255);
if (pulses1 >= 10) {
pwmvalue1 = 255;
}
Serial.print("Pulses in half a second (Pulse 2): ");
Serial.println(pulses2);
pwmvalue2 = map(pulses2, 0, 10, 0, 255);
if (pulses2 >= 10) {
pwmvalue2 = 255;
}
pulses1 = 0;
pulses2 = 0;
}
like i said, the code kind of works but it is pretty Janky.
all help is greatly appreciated!!
Thanks