EDITED: 'pwm' was not declared in this scope. (using Timer1 library)

Hi guys.
this is my first time (OH HELLO! :slight_smile: ) coding for my arduino duemilanove and it seems that I have hit a problem.
I have googled the problem but it have solved some but not all of them. =(
I would like to ask some help from you guys since I don’t really know what to do now.
here is the code and the error message.
I also uses Timer1 library to generate PWM.

thanks :slight_smile:

#include <TimerOne.h>

int SWspeed0 = 0;

int SWpushout = 1;
int SWpushin = 2;

int SWstopin = 3;
int SWstopout = 4;

int LEDin = 5;
int LEDout = 6;

int MotorDirection01 = 9;
int MotorDirection02 = 8;
int MotorDirection03 = 10;
int MotorDirection04 = 11;

void setup()
{
pinMode(SWspeed0, INPUT);

pinMode(SWstopin, INPUT);
pinMode(SWstopout, INPUT);

pinMode(MotorDirection01, OUTPUT);
pinMode(MotorDirection02, OUTPUT);
pinMode(MotorDirection03, OUTPUT);
pinMode(MotorDirection04, OUTPUT);

pinMode(LEDin, OUTPUT);
pinMode(LEDout, OUTPUT);

Timer1.initialize(1000000);
}

void loop()
{
speed0 = digitalRead(SWspeed0);
pushin = digitalRead(SWpushin);
pushout = digitalRead(SWpushout);
stopin = digitalRead(SWstopin);
stopout = digitalRead(SWstopout);

if (pushout == HIGH)
{
if (stopout == LOW)
{
if (speed0 == HIGH)
{
pwm(9,512);
digitalWrite(MotorDirection02,HIGH);
}
else
{
pwm(9,128);
digitalWrite(MotorDirection02,HIGH);
}
}
else
{
}
}
else
{
}
if (pushin == HIGH)
{
if (stopin == LOW)
{
if (speed0 == HIGH)
{
pwm(10,512);
digitalWrite(MotorDirection04,HIGH);
}
else
{
pwm(10,128);
digitalWrite(MotorDirection04,HIGH);
}
}
else
{
}
}
else
{
}
}

The error message:

arduinocode1.cpp: In function ‘void loop()’:
arduinocode1:39: error: ‘speed0’ was not declared in this scope
arduinocode1:40: error: ‘pushin’ was not declared in this scope
arduinocode1:41: error: ‘pushout’ was not declared in this scope
arduinocode1:42: error: ‘stopin’ was not declared in this scope
arduinocode1:43: error: ‘stopout’ was not declared in this scope
arduinocode1:51: error: ‘pwm’ was not declared in this scope
arduinocode1:56: error: ‘pwm’ was not declared in this scope
arduinocode1:73: error: ‘pwm’ was not declared in this scope
arduinocode1:78: error: ‘pwm’ was not declared in this scope

You need to declare the missing variables like the ones above "setup()"

You haven't defined "PWM()" at all

thanks!

AWOL: You need to declare the missing variables like the ones above "setup()"

You haven't defined "PWM()" at all

How do I define it? writing "PWM()" before the setup () ?

How do I define it? writing "PWM()" before the setup () ?

Come on, don't you see the "reference" link on the home page ? :-)

http://arduino.cc/en/Reference/AnalogWrite

thanks, I wanted to use this library, http://arduino.cc/playground/Code/Timer1

but I have tried analogWrite and it can compile successfully. looks like that have to do for now though if Timer1 can be used,it will be a great addition :D

Sorry, my bad for not seeing that maybe you were trying to use Timer1’s pwm() method… I think you have to prepend Timer1. to pwm(), like this:

Timer1.pwm()

HTH

edit: also, when posting code, use “#” (code) button, not the quote one. :slight_smile:

thanks,tuxduino.
I just realized that the methods that I want to use in Timer1 should be used in void setup(),not in void loop().
this is my new code.
is it okay for me to leave the field in the bracket for ‘else’ blank?
because I don’t want the program to do anything until all the conditions I set are met.

I started using pins from pin 0.
is it the digital pin 0 on the arduino?

#include <TimerOne.h>

int SWspeed0 = 0;

int SWpushout = 1;
int SWpushin = 2;

int SWstopin = 3;
int SWstopout = 4;

int LEDin = 5;
int LEDout = 6;

int MotorDirection01 = 9;
int MotorDirection02 = 8;
int MotorDirection03 = 10;
int MotorDirection04 = 11;

int speed0;
int pushin;
int pushout;
int stopin;
int stopout;

int duty1;
int duty2;
void setup()
{
pinMode(SWspeed0, INPUT);

pinMode(SWpushout, INPUT);
pinMode(SWpushin, INPUT);

pinMode(SWstopin, INPUT);
pinMode(SWstopout, INPUT);

pinMode(MotorDirection01, OUTPUT);
pinMode(MotorDirection02, OUTPUT);
pinMode(MotorDirection03, OUTPUT);
pinMode(MotorDirection04, OUTPUT);

pinMode(LEDin, OUTPUT);
pinMode(LEDout, OUTPUT);

Timer1.initialize(1000000);
Timer1.pwm(9,duty1);
Timer1.pwm(10,duty2);
}

void loop()
{
speed0 = digitalRead(SWspeed0);
pushin = digitalRead(SWpushin);
pushout = digitalRead(SWpushout);
stopin = digitalRead(SWstopin);
stopout = digitalRead(SWstopout);

if (pushout == HIGH) 
  {
  if (stopout == LOW)
    {
      if (speed0 == HIGH)
        {
         duty1 = 512;
         digitalWrite(MotorDirection02,HIGH);
        }
        else 
         {
          duty1 = 256;
          digitalWrite(MotorDirection02,HIGH);
         }
    }
     else
       {         
       }
  }
    else
      {          
      }
if (pushin == HIGH) 
  {
  if (stopin == LOW)
    {
      if (speed0 == HIGH)
        {
         duty2 = 512;
         digitalWrite(MotorDirection04,HIGH);
        }
        else
         {
          duty2 = 256;
          digitalWrite(MotorDirection04,HIGH);
         }
    }
     else
       {
       }
  }
    else
      {
      }
}

As I said, I've never used Timer1 library, but I doubt using pwm() the way you did will work. When you change duty1 value, for example, you have to call pwm() again, or the new duty value won't be set. I suspect you think that passing duty1 to Timer1.pwm() will in some way bind duty1 to Timer1's pwm code. That's not how function calls work. When you call Timer1.pwm(9, duty1) you simply pass pwm() the value that duty1 has at the time the call is made. If duty1 value changes later, the code in side pwm() won't notice at all.

Pins 0 and 1 belong to the hardware serial port, and are the ones through which compiled sketches are uploaded to the Arduino. Therefore it's better to leave them alone and use pins from 2 onwards.

You can leave empty else {} in your code, but that will just make it less readable. Take them away and use CTRL-T in the Arduino IDE instead to obtain clearer code.