Here is a schematic of my project. I am trying to control the speed of a drive motor. I have 2 hall sensors Hall1 will read 1 pulse per shaft rotation, and Hall2 will read 4 pulses per shaft rotation. I have a grove push button I am using to start and stop the motor. I have 1 LED Green that will light up when the system is ready and running at the required RPM either 1800 or 3600 which will be different than the motor. I have another LED Red that I want to light up when the current I am reading from the Pololu 2994 motor driver CS terminal gets above a certain amperage, the direction of the motor does not matter as long as it operates at the required RPM of the shaft the motor is attached too which means the motor will operate at a higher speed than the shaft which will be lower because of 3 to 1 60 teeth on the shaft to 20 teeth on the motor. I have managed to do some of the code but I cannot figure out how to code the PWM with an easy and adjustable setting. for the target rpm which is either 1800 rpm or 3600 rpm for the shaft Any help would be highly appreciated.
Here is what I have for code, I was able to find a simple PWM code along with a button and sensor coding which I can understand and follow. I have to say this code stuff is FUN even though it drives me crazy, every little mistake causes problems but I can usually find them. The sensor code may or may not be working correctly to hard to tell the serial monitor changes too fast, any way to slow it down or have just 1 line that will show the fluctuations? The button code was working fine until I added the code for the driver now it doesn't want to shut off and I don't know why. The other problem I have and the reason I want the Arduino to adjust the RPM is the voltage to the motor will take a jump from 12-13 volts to 14-15 volts. I wouldn't be doing this but If I cannot get the code and Arduino to do what I want and need I do not want to buy the Lathe and Mill I have on order, I have time to cancel it they are back ordered right now. I also want to let everyone know I do plan on using the 16x2 LCD screen that came with the starter kit but do I have enough pins left over I just want to show volts amps and RPMs. if possible any help with that would also be highly appreciated. Thank You in advance!! Steve
#define LED_OVR 11 // Over current LED
#define DIR 10 // direction input from Pololu 2994 motor driver
#define PMCU_ON 13 // Output to on input for PMCU arduino board
#define CRNT A0 // CS Analog input from pololu 2994 motor driver
const int PSH_ON = 8; //Push Button input 1st push start 2nd stop
const int LED_ON = 12; // Led output showing everything is operating ok
int ledflag=0; // LED status
int PWM = 9;
int HALL1 = 2; // assigning halleffect sensor 1 to pin 2
int HALL2 = 3; // assigning halleffect snesor 2 to pin 3
byte backlight = 0, rs = 1, rw = 2, enable = 3,
d4 = 4, d5 = 5, d6 = 6, d7 = 7;
// using the byte fucntion, the microprocessor is acknowledging the pin connections from the arduino board to the lcd connections
// note: only 10 out of 16 pins on board are used for display
//see diagram of lcd for pin locations
LiquidCrystal lcd(rs, rw, enable, d4, d5, d6, d7);
volatile byte half_revolutions; // measurement of how many revolutions are counted
unsigned int rpm; //this will be the units
unsigned long timeold; // lenght of time
// you can change which pins your halleffect sensor is connected to just reassign the pin numbers
void setup() {
Serial.begin(115200);
pinMode(HALL1, INPUT);
pinMode(HALL2, INPUT);
pinMode(LED_ON, OUTPUT);
pinMode(LED_OVR, OUTPUT);
pinMode(PWM, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(PSH_ON, INPUT);
pinMode(PMCU_ON, OUTPUT);
pinMode(CRNT, INPUT);
//pinMode(6, OUTPUT);
//pinMode(backlight, OUTPUT);
//digitalWrite(backlight, HIGH);
//lcd.begin (20,4); // telling processor how many rows and columns the LCD has
//lcd.setCursor (0,0); //telling the processor where to begin
//lcd.print (“Carrier RPM”);
//lcd.setCursor (0,1);
//lcd.print(DEC);
//lcd.setCursor(0,2);
//lcd.print (“Output RPM”);
//lcd.setCursor (0,3);
//lcd.print(DEC);
attachInterrupt(0, HALL1, RISING); // connects interrupt to pin 2
// interrup allows you to “multitask” – will switch off one function to show another funtion
// this processor cannot run more than one function at once
attachInterrupt(3, HALL2, RISING); // attaches intterupt to pin 3
half_revolutions=0; // These are all your starting points
rpm=30;
timeold=0;
}
void loop() {
if (digitalRead(PSH_ON)==HIGH){ // if button is pressed
if (ledflag==0) { // and the status flag is LOW
ledflag=1; // make status flag HIGH
digitalWrite(LED_ON, HIGH);
digitalWrite(PWM, HIGH); // and turn on the LED
} //
else { // otherwise...
ledflag=0; // make status flag LOW
digitalWrite(LED_ON, LOW);
digitalWrite(PWM, LOW); // and turn off the LED
}
delay(1000); // wait a sec for the hardware to stabilize begin again
}
int sensorValue = analogRead(A0);
float current = sensorValue * (5 / 1023.0);
Serial.println(current);
if ((sensorValue)==sensorValue+2){
digitalWrite(PWM, LOW);
digitalWrite(LED_OVR, HIGH);
}
attachInterrupt(2, HALL1, RISING); // connects interrupt to pin 2
// interrupt allows you to “multitask” – will switch off one function to show another funtion
// this processor cannot run more than one function at once
attachInterrupt(3, HALL2, RISING); // attaches intterupt to pin 3
half_revolutions=0; // These are all your starting points
rpm=0;
timeold=0;
if (half_revolutions>=20) {
rpm = 30*1000/(millis()-timeold)*half_revolutions;
timeold=millis();
half_revolutions=0;// this section is telling the processor how to convert tO rpm
}
} ~~~
