Timer1 Help

Hi everyone,
This is the first code I have written (hopefully my ruggeduino will come in tomorrow). :astonished:

The plan:
POT input on pin 0 to control PWM %
PWM output on pin 9 @ fixed 50Hz

Here is the thread for my project.
http://arduino.cc/forum/index.php/topic,69965.0.html

I am looking for some help with Timer1 functions.

Just recently replaced analogWrite() with setPwmDuty()
Now when I verify code I return this error.
pwm_solenoid.cpp: In function 'void loop()':
pwm_solenoid:38: error: 'setPwmDuty' was not declared in this scope

Here is the code as I have it right now.

#include <TimerOne.h>

// these constant variables store the pin numbers
  const int solenoidPin = 9;  // Solenoid connected to digital pin 9
  const int knobPin = 0;      // potentiometer wiper (middle terminal) connected to analog pin 0
                              // outside terminals to ground and +5V
  int knobValue, pwmValue;    // these variables store the values for the knob and PWM level

void setup()  { 
  Serial.begin(9600);        // initialize the serial port
  Timer1.initialize(20000);  // Set a timer of 50Hz
} 

void loop()  { 
  knobValue = analogRead(knobPin);              // read the value from the input
  pwmValue = map(knobValue, 0, 1023, 0, 254);   // remap the values from 10 bit input to 8 bit output
  setPwmDuty(solenoidPin, pwmValue);            // use the input value to control the solenoid
  Serial.println(pwmValue);                     // print the input value to the serial port for debugging
}

Could someone give me a hand with what I have right and what needs to be changed.
Also is Timer1.initialize() set up correctly for operation @ 50Hz?

Thanks

you must put the function in a file e.g. : solenoid.h and #include "solenoid.h" in your .pde file...

robtillaart:
you must put the function in a file e.g. : solenoid.h and #include "solenoid.h" in your .pde file...

You will have to go into some more depth if you want me to follow what you are trying to say.
What issue are you referring to?
I have saved this as a .pde file.
You want me to import another library? solenoid.h?

Just recently replaced analogWrite() with setPwmDuty()

In which file did you declare this function?

As your PDE file does not define setPWMDuty() I was under the assumption that is was a file pwm_solenoid.cpp or so and it was not included...

You know all your "main" sketches should have a .pde extension ; not .cpp?

You could include the function in your sketch iso

#include <TimerOne.h>

// these constant variables store the pin numbers
  const int solenoidPin = 9;  // Solenoid connected to digital pin 9
  const int knobPin = 0;      // potentiometer wiper (middle terminal) connected to analog pin 0
                              // outside terminals to ground and +5V
  int knobValue, pwmValue;    // these variables store the values for the knob and PWM level

void setup() 
{ 
  Serial.begin(9600);        // initialize the serial port
  Timer1.initialize(20000);  // Set a timer of 50Hz
} 

void loop()  
{ 
  knobValue = analogRead(knobPin);              // read the value from the input
  pwmValue = map(knobValue, 0, 1023, 0, 254);   // remap the values from 10 bit input to 8 bit output
  setPwmDuty(solenoidPin, pwmValue);            // use the input value to control the solenoid
  Serial.println(pwmValue);                     // print the input value to the serial port for debugging
}

setPwmDuty(int pin, uint8_t value)
{
  analogWrite(pin, value);
  // optionally add more code here
}

robtillaart:

Just recently replaced analogWrite() with setPwmDuty()

In which file did you declare this function?

As your PDE file does not define setPWMDuty() I was under the assumption that is was a file pwm_solenoid.cpp or so and it was not included...

You know all your "main" sketches should have a .pde extension ; not .cpp?

I am not sure we are talking on the same wavelength here....
I have created this sketch as one file with an extension .pde.
I have no idea why the error is describing pwm_solenoid.cpp, this file does not exist.
#TimerOne.h is included in this sketch for use of its functions to allow a fixed 50Hz.

analogWrite() was omitted for use of setPWMDuty() due to some advise I received to use Timer1 function instead of analogWrite.
Before this when I verified the code there were no errors shown.

robtillaart:
You could include the function in your sketch iso

#include <TimerOne.h>

// these constant variables store the pin numbers
 const int solenoidPin = 9;  // Solenoid connected to digital pin 9
 const int knobPin = 0;      // potentiometer wiper (middle terminal) connected to analog pin 0
                             // outside terminals to ground and +5V
 int knobValue, pwmValue;    // these variables store the values for the knob and PWM level

void setup()
{
 Serial.begin(9600);        // initialize the serial port
 Timer1.initialize(20000);  // Set a timer of 50Hz
}

void loop()  
{
 knobValue = analogRead(knobPin);              // read the value from the input
 pwmValue = map(knobValue, 0, 1023, 0, 254);   // remap the values from 10 bit input to 8 bit output
 setPwmDuty(solenoidPin, pwmValue);            // use the input value to control the solenoid
 Serial.println(pwmValue);                     // print the input value to the serial port for debugging
}

setPwmDuty(int pin, uint8_t value)
{
 analogWrite(pin, value);
 // optionally add more code here
}

Please keep in mind I do not have the Arduino hardware yet and this is is the first code I have written.
Could you please describe what function you are referring to and its purpose.
If anyone else with another perspective could chime in and provide some help I would appreciate it.
Everyone takes a different angle, it would be nice to hear some additional advise.

Thanks

Suggest you wait until you do have some hardware.
People here will help with real problems, but are unlikely to try out your code for you.

For such a short sketch,

pwm_solenoid:38: error: 'setPwmDuty' was not declared in this scope

, error messages don't get much more specific.

AWOL:
For such a short sketch,

pwm_solenoid:38: error: 'setPwmDuty' was not declared in this scope

, error messages don't get much more specific.

Could you tell me how to correct this error?

Or should I run the code as is once the hardware arrives?

void TimerOne::setPwmDuty(char pin, int duty)
{
unsigned long dutyCycle = pwmPeriod;
dutyCycle *= duty;
dutyCycle >>= 10;
if(pin == 1 || pin == 9) OCR1A = dutyCycle;
else if(pin == 2 || pin == 10) OCR1B = dutyCycle;
}

Did you try to call Timer1.setPwmDuty(char pin, int duty) as soon as it is TimerOne method?