Go Down

Topic: PWM DC Motor control with Counter problem (Read 2275 times) previous topic - next topic

lsburden

In an effort to troubleshoot, I did something that I was advised not to do.
I moved the serial print count to the function called by the interrupt.
I also commented out other serial print commands as I really am only concerned with count.

The count is now accurate but does not stop at zero...it keeps going into negative numbers. The loop is supposed to disable the motor driver when the count = 0. That said, it looks to me like the program may need a new approach.

I also changed count to a "volatile int" and increased the baud now that I know that I need to update the serial monitor baud too.



Code: [Select]
/*
  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;
volatile 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 = 100;
  pinMode(EnablePin, OUTPUT);  
  pinMode(PWMPin, OUTPUT);  
  pinMode(PWMPin2, OUTPUT);  
  setPwmFrequency(PWMPin, 8);  //change Timer2 divisor to 8 gives 3.9kHz PWM freq
  Serial.begin(38400);
  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--;
  Serial.print("\n count = ");
  Serial.print(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
  }
}

 

lsburden

schematic attached

lsburden

Looks like the latest version of my code works. The problem is that the motor doesn't stop rotating when PMW = 0 and enable. I need to factor this in and/or electric brake.

Go Up