Thanks for looking at this. I have now gone to the other extreme - defined every variable possible so it does compile but does not produce the PWM output signal. Also think there is at least one } too many though only compiles with all of these in it. Anything you spot would be welcome...
sketch code:
//Purpose: To produce a PWM signal output at 20Hz frequency and a duty cycle that has a minm of 15% and max'm of 85%, the pot adjusted for any value in bewtween
// Arduino UNO board design: Has an input pot resistance value from pot wiper into pin A0 (pot has VCC from 5v pin and Ov from Grd pin). PWM signal ouput defined on pin 9
// Testing: currently using oscillosocpe on output pin 9, unloaded.
// Previous code worked at 30.5Hz, successfully varying LED brightness but frequency was too high for motor controller to work and turn the motor.
// STATUS: DOES COMPILE SUCCESSFULLY BUT NO PWM OUTPUT SIGNAL NOW PRODUCED ON PIN 9.
const int potPin = A0; // Analog input pin that has the potentiometer wiper resistance value
const int outPin = 9; // PWM output pin that Motor Controller) (or LED in testing) is attached to
unsigned long dutyCycle; // Defines the duty cycle, the % of the period (the cycle time) the PWM pulse is high
unsigned long period; // Defines the cycle time ( 1 / frequency) for PWM output
unsigned long lastChange; // The variable for incrementing the time through the loop
unsigned long signal; // The variable holding the value of Hi or Lo output that is derived through the loop
void setup() {
unsigned int period = 50; // 50 ms period, to be used to set the output signal at 20Hz frequency
unsigned long lastChange = 0; // Not sure why need lastchange defined in code above with this here - but does not compile without it!
float dutyCycle; // 20% duty cycle. // Again, Not sure why need dutyCycle defined in code above with this here - but does not compile without it!
bool signal = LOW;
}
void loop() {
dutyCycle = map(analogRead(potPin), 0, 1023, 1500, 8500) / 10000.0; // returns 15% to 85% duty cycle range based on pot rotation reading 0-1023.
unsigned int onTime = period * dutyCycle; // defines the high duration in mS
unsigned int offTime = period * (1 - dutyCycle); // defines the low duration in mS
if (signal == HIGH) {
if (millis() - lastChange > onTime) {
signal = LOW;
lastChange += onTime;
}
else {
if (millis() - lastChange > offTime) {
signal = HIGH;
lastChange += offTime;
}
}
digitalWrite(outPin, signal);
}
}
Thanks again for reviewing this.