I'm no programmer, and (sometimes) know just enough to get myself by. Currently I'm struggling with a division operator that leaves the resultant dutyCycle = 0
I have tried many different variable types, casting, position of function, but nothing helps. Maybe one of you actual programmers could spot my error?? In function sum(), dutyCycle always stays at zero. tempPulseWidth and tempPhaseLength both range from 0-100
My code reads in a PWM signal and simply reports a duty-cycle. This will be used to create its own PWM signal to power a hobby ESC, turning a motor, which is the drive motor for my spindle on a small home-made CNC mill.
Hopefully all questions starting with "Why did you.... " are already answered by my first sentence.
// LCD feedback
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// ESC
#include <Servo.h>
Servo spindle;
// Variables
byte i = 0;
byte j = 0;
byte watchDog = 0;
unsigned int spindleLo = 95;
unsigned int spindleHi = 165;
unsigned int spindleSpeed = 95;
unsigned int dutyCycle = 0;
unsigned int timePhaseLength[10] = {};
unsigned int timePulseWidth[10] = {};
unsigned long timeRisingOld = 0;
unsigned long timeRisingNew = 0;
unsigned long timeFalling = 0;
void setup() {
//
Serial.begin(115200);
//
// Begin LCD
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("**CNC SPINDLE**");
//
// Begin ESC
lcd.setCursor(0, 1);
lcd.print("init output... ");
spindle.attach(6);
spindle.write(spindleSpeed);
delay(1000);
lcd.setCursor(0, 1);
lcd.print("cal. output... ");
for (int i = 0; i<=180; i++) {
spindle.write(i);
delay(75);
}
spindleSpeed=spindleLo;
spindle.write(spindleLo);
//
// Interrupt Config
lcd.setCursor(0, 1);
lcd.print("pcint config... ");
pinMode(14,INPUT);
PCICR = _BV(PCIE1);
PCMSK1 = _BV(PCINT8);
delay(500);
//
// Done
lcd.clear();
lcd.print(" Complete ");
delay(1000);
lcd.clear();
lcd.print("Spindle Pwr:");
}
void loop() {
if (watchDog) {
watchDog=0;
} else {
timePhaseLength[i] = 0; i++; if (i>=10) i=0; // Run numbers back down to 0 if no PWM detected
timePulseWidth[j] = 0; j++; if (j>=10) j=0; //
}
delay(100);
sum();
// spindleSpeed = map(dutyCycle,0,100,spindleLo,spindleHi);
// lcd.setCursor(12, 0);
// lcd.print(dutyCycle);
// lcd.print("% ");
// spindle.write(spindleSpeed);
}
ISR(PCINT1_vect) {
watchDog=1;
if (digitalRead(14)) {
// IF RISING EDGE
timeRisingOld = timeRisingNew;
timeRisingNew = millis();
timePhaseLength[i] = constrain((timeRisingNew-timeRisingOld),0,500);
i++;
if (i>=10) i=0;
} else {
timeFalling = millis();
timePulseWidth[j] = constrain((timeFalling-timeRisingNew),0,500);
j++;
if (j>=10) j=0;
}
}
void sum() {
unsigned int tempPhaseLength = 0;
unsigned int tempPulseWidth = 0;
for (int x=0; x<10; x++) tempPhaseLength += timePhaseLength[x];
for (int x=0; x<10; x++) tempPulseWidth += timePulseWidth[x];
tempPulseWidth = (tempPulseWidth / 10);
tempPhaseLength = (tempPhaseLength / 10);
if (tempPhaseLength == 0) tempPhaseLength = 1;
dutyCycle = tempPulseWidth/tempPhaseLength;
//
Serial.println(dutyCycle);
Serial.println(tempPulseWidth);
Serial.println(tempPhaseLength);
}