Pages: [1]   Go Down
Author Topic: [SOLVED] Arduino IDE 1.01 preprocessor commands  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I adjust my preprocessor commands (e.g. SQUELCH was defined as 1 then I changed it to 0) I get a bunch of errors such as:
Code:
ATCMUS.cpp: In function 'void setup()':
ATCMUS:19: error: 'orangeLED' was not declared in this scope
ATCMUS:32: error: 'LEDoff' was not declared in this scope
ATCMUS:37: error: 'LEDoff' was not declared in this scope
ATCMUS.cpp: In function 'void loop()':
ATCMUS:49: error: 'greenLED' was not declared in this scope
ATCMUS:67: error: 'redLED' was not declared in this scope
when they were just fine before. Why is it suddenly not seeing these functions.  I had the same errors with digitalWrite() and other basic arduino constructs (HIGH, LOW, INPUT, etc) until I included Arduino.h.  I've never had to seperately declare my functions (void orangeLED(); ) before and the preprocessors make it much easier for me to change the program based on my requirements.

Code below:
Code:
#include <Arduino.h>

#define DEBUG 1
#define SQUELCH 0

#if SQUELCH
  int potPin = A0; // select the input pin for the potentiometer
#endif

int sensorPin = A5;    
int rled = 6;
int gled = 8;
int relay = 2;
int overridepin = 9;

void setup()
{
  pinMode(rled, OUTPUT);
  pinMode(gled, OUTPUT);
  orangeLED(); //indicate setup
  pinMode(relay, OUTPUT);
  pinMode(sensorPin, INPUT);
  pinMode(overridepin, INPUT_PULLUP);
  #if SQUELCH
    pinMode(potPin,INPUT);
  #endif
  
  #if DEBUG
    Serial.begin(9600);
  #endif
  for (int i=0;i<4;i++)//warn setup finishing
  {
    LEDoff();
    delay(250);
    orangeLED();
    delay(250);
  }
  LEDoff();
}

void loop()
{
  float sensitivity = 0.00;
  int sensorValue = 0;
  int sensValold = 0;
  float diff = 0.00;
  
  if (digitalRead(overridepin)==LOW)
  {
    greenLED();
    digitalWrite(relay, LOW);
    
    #if DEBUG
      Serial.println("Override Engaged");
    #endif
    
    delay(2000);
  }
  else
  {
    #if SQUELCH
      [redacted]
    #else
      [redacted]
    #endif
    
    redLED();
    sensorValue = analogRead(sensorPin);
    diff = (float)(sensorValue - sensValold)/(float)(sensorValue);
    
    #if DEBUG
      Serial.println(abs(diff));
    #if SQUELCH
      Serial.print("Sensitivity: ");
      Serial.println(sensitivity);
    #endif
    #endif
    
    if (abs(diff) > sensitivity)
    {
      digitalWrite(relay, LOW);
      delay(2000);
    }
    else
    {  
      digitalWrite(relay, HIGH);
    }
    sensValold = sensorValue;
    delay(75);
  }  
}
void redLED()
{
  digitalWrite(rled,HIGH);
  digitalWrite(gled,LOW);
}
void greenLED()
{
  digitalWrite(gled,HIGH);
  digitalWrite(rled,LOW);
}
void orangeLED()
{
  digitalWrite(rled,HIGH);
  digitalWrite(gled,HIGH);
}
void LEDoff()
{
  digitalWrite(rled,LOW);
  digitalWrite(gled,LOW);
}
« Last Edit: October 25, 2012, 03:56:46 pm by kjablonski » Logged

Offline Offline
Edison Member
*
Karma: 31
Posts: 1418
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This a known artifact of the way that the Arduino IDE processes source files. It goes through your code and puts function declarations after what it considers to be the first executable statement in your file.
You have:
Code:
#if SQUELCH
  int potPin = A0; // select the input pin for the potentiometer
#endif
It puts the declarations after the declaration of potPin. The problem is that it does that even if potPin is inside an #ifdef that is not included.
The easy workaround is to put a statement like this:
Code:
char junk;
*before* anything else.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I declared my functions right after my include and now it works regardless of my defines.  Thanks for the help!
Code:
#include <Arduino.h>
void orangeLED();
void redLED();
void greenLED();
void LEDoff();

#define DEBUG 1
#define SQUELCH 0
...snipity do dah...
}
void orangeLED()
{
    //do stuff
}
...snipity day....
Logged

Pages: [1]   Go Up
Jump to: