This is my first Arduino project and hoping someone can help me out...
I am using an Aduino Uno and a Robot Power MegaMoto to drive a DC motor from a drill. I have a pot to control the speed and using a hall effect sensor to count revolutions.
The problem I am having is that it doesn't look like I am getting an accurate count. I am using the Serial Monitor to watch the count, PWM duty cycle, and the value from the pot.
The sketch is copied in this post as well as attached:
(hope someone can help...)
Thanks,
Lucas
/*
Coil Winder sketch
Potentiometer to control speed.
TLE4095 Hall Effect Sensor as a counter.
MegaMoto shield used to drive a DC Motor.
*/
int EnablePin = 8;
int duty;
int PWMPin = 11; // Timer2
int PWMPin2 = 3;
int potPin = A0;
int potValue = 0;
int count;
const byte CPin = 0; // analog input channel
int CRaw; // raw A/D value
float CVal; // adjusted Amps value
void setup(){
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
count = 50;
pinMode(EnablePin, OUTPUT);
pinMode(PWMPin, OUTPUT);
pinMode(PWMPin2, OUTPUT);
setPwmFrequency(PWMPin, 8); //change Timer2 divisor to 8 gives 3.9kHz PWM freq
Serial.begin(9600);
attachInterrupt(0, magnet_detect, RISING);//Initialize the interrupt pin (Arduino Digital pin 2)
}
void loop()
{
// To drive the motor in H-bridge mode
// the power chip inputs must be opposite polarity
// and the Enable input must be HIGH
if (count > 0)
{
digitalWrite(EnablePin, HIGH); // Pin 8 high, enable motor driver
analogWrite(PWMPin2, 0); // Set Pin 3 low
potValue = analogRead(potPin); // Read voltage from pot wiper
duty = map(potValue, 0, 663, 0, 255);
analogWrite (PWMPin, duty);
Serial.print("count = ");
Serial.print(count);
Serial.print("\t potentiometer = " );
Serial.print(potValue);
Serial.print("\t motor = ");
Serial.println(duty);
// delay(2);
}
else
{
digitalWrite(EnablePin, LOW); // Pin 8 low, disable motor driver
}
}
/*
- Divides a given PWM pin frequency by a divisor.
- The resulting frequency is equal to the base frequency divided by
- the given divisor:
-
- Base frequencies:
- o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
- o The base frequency for pins 5 and 6 is 62500 Hz.
-
- Divisors:
- o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
- 256, and 1024.
- o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
- 128, 256, and 1024.
- PWM frequencies are tied together in pairs of pins. If one in a
- pair is changed, the other is also changed to match:
-
- Pins 5 and 6 are paired (Timer0)
-
- Pins 9 and 10 are paired (Timer1)
-
- Pins 3 and 11 are paired (Timer2)
- Note that this function will have side effects on anything else
- that uses timers:
-
- Changes on pins 5, 6 may cause the delay() and
- millis() functions to stop working. Other timing-related
- functions may also be affected.
-
- Changes on pins 9 or 10 will cause the Servo library to function
- incorrectly.
- Thanks to macegr of the Arduino forums for his documentation of the
- PWM frequency divisors. His post can be viewed at:
-
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
*/
void magnet_detect() //This function is called whenever a magnet/interrupt is detected
{
count--;
}
void setPwmFrequency(int pin, int divisor)
{
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { // Timer0 or Timer1
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode; // Timer0
} else {
TCCR1B = TCCR1B & 0b11111000 | mode; // Timer1
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode; // Timer2
}
}
CoilWinder.ino (3.82 KB)