Good Afternoon all.
Ive created an project based on the Timer1. I do have a poblem where the code for some reason lag the processor and the response to the input 12 seems to take few good seconds to update the state. Unfortunetly i never worked with the PWM library therefore i
m not so sure if this is cause by my hardware or maybe someone else is able to spot an error in my code please?
MC-2100 Treadmill Motor Controller Interface
Lathe Motor Controller via PWM
Seven Segment Tachometer
ON/OFF Toggle
Joe Schoolcraft
Brian Schoolcraft
May 2013
https://sonsofinvention.wordpress.com/
*/
#include "TimerOne.h" //http://playground.arduino.cc/code/timer1
#define POT_READ A0 //Wiper of pot connected as voltage divider (Speed Command Input)
#define SPEED_SENSE 2 //connected to reed switch - magnet closes switch, pulls to ground (Spindle Speed Input - 2 pulses per rev)
#define PWM_OUT 9 //Connected to blue wire of MC2100 (50ms period PWM out)
#define PWM_CYCLE 50.0 //Output Signal PWM Period (50ms)
#define POT_DIF 4 //Change detection threshold on pot
#define MAX_DUTY 869 //Max Duty Cycle expected by MC-2100 (85% of 1023)
#define MIN_DUTY 0 //Min Duty Cycle expected by MC-2100 (0% of 1023)
int potTemp;
int potValue;
int lastPotValue;
int potCheck;
int speedLevel;
int buttonState; // the current reading from the input pin
int lastButtonState = LOW;
const int ON_OFF = 13; //on off switch input
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50;
byte onOffState = 0;
void setup()
{
Serial.begin(9600);
pinMode(POT_READ, INPUT);
pinMode(PWM_OUT, OUTPUT);
pinMode(ON_OFF, INPUT_PULLUP); //Enable internal pullup resistor to simplify external circuit
Timer1.initialize(PWM_CYCLE*1000); //Set pin 9 and 10 period to 50 ms
Timer1.pwm(PWM_OUT, 0); //Start PWM at 0% duty cycle
}
void loop()
{
int reading = digitalRead(ON_OFF);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
onOffState = !onOffState;
}
}
}
digitalWrite(ON_OFF, onOffState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
//Read and condition pot value
potTemp = analogRead(POT_READ);
potCheck = abs(potTemp - potValue);
if(potCheck >= POT_DIF) { //Only accept new value if it's far enough from the current accepted value
potValue = potTemp;
}
speedLevel = map(potValue,0,1023,0,MAX_DUTY); //Convert Pot input to pwm level to send to MC-2100
Serial.println(onOffState);
if (onOffState == 0){ //Off
Timer1.setPwmDuty(PWM_OUT, 0); //Shut down MC-2100
}
if (onOffState == 1){ //ON
Timer1.setPwmDuty(PWM_OUT, speedLevel); //Send speed command to MC-2100
}
}