Attempting to use Solar Tracker code for stepper motor . Specifically: pololu.com/viewtopic.php?f=15&t=6424. Attempting to compile for UNO, I get consistent errors that directionVAL was not declared in this scope. I have tried changing from “boolean directionVAL = HIGH” to true or 1. Same problem. I tried changing boolean to int declaration and directionVAL = 1. I have reviewed the use of boolean variables. I am missing something here.
My IDE is 2.3.7, libraries updated, using a Real Arduino 3.
Hello.
The link you included to your code does not seem to be working. Can you post a new link, or attach the code directly to a new post?
- Patrick
/********
Code based upon:
http://forum.pololu.com/viewtopic.php?f=15&t=6424
******/
#include <Stepper.h>
#define DEBUG 1 // Used to toggle debug code
#define SETTLESTEPPER 10 // Time for stepper to change state
#define READSENSORDELAY 5 // Time for sensors to stabilize
#define TOPSENSORPIN A0 // Pin for top sensor
#define BOTTOMSENSORPIN A2 //Pin for bottom sensor
#define SLEEPPIN 11 // Sleep pin on driver
#define RESETPIN 12 // Reset pin on driver
#define STEPPIN 9 // Step pin on driver
#define DIRECTIONPIN 8 // Direction pin on driver
#define ENABLEPIN 10 // Enable pin on driver
#define SENSORSENSITIVITY 60 // Sensor difference that causes a rotation
#define STEPPERINCREMENT 10 // This many steps (18 degrees) to move panel
int directionVAL =0;
int topPhotoValue =0; // Reading from top photoresistor
int bottomPhotoValue = 0; // Reading from bottom photoresistor
int previousValue = 0; // Previous reading from sensor
// =============================== setup90 ===============
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
#endif
pinMode(SLEEPPIN,OUTPUT); // Initialize stepper pin on uC
pinMode(RESETPIN,OUTPUT);
pinMode(DIRECTIONPIN,OUTPUT);
pinMode(STEPPIN,OUTPUT);
pinMode(ENABLEPIN, OUTPUT);
pinMode(TOPSENSORPIN, INPUT); //Set sensor pins
pinMode(BOTTOMSENSORPIN, INPUT);
digitalWrite(SLEEPPIN, HIGH); //Make sure defaults are set
digitalWrite(RESETPIN, HIGH);
}
// ======================= LOOP() ==========================
void loop()
{
void ReadSensors(); // Sensor read to get top and bottom
// sensor values
#ifdef DEBUG
Serial.print("bottom sensor = ");
Serial.print(bottomPhotoValue);
Serial.print(" top sensor = ");
Serial.print(topPhotoValue);
Serial.print(" dir pin = ");
Serial.print(directionVal);
Serial.print(" previous value = ");
Serial.println(previousValue);
#endif
if (abs(bottomPhotoValue - topPhotoValue) > SENSOESENSITIVITY)
{
RotateStepper(stepperincrement);
delay(1000);
}
previousValue = topPhotoValue - bottomPhotoValue;
if (previousValue < 0) // Wrong direction?
{
directionVal = !directionVal;
while (previousValue < 0) {
RotateStepper(STEPPERINCREMENT);
ReadSensors();
previousValue = topPhotoValue - bottomPhotoValue;
#ifdef DEBUG
Serial.print("**************** bottom sensor = ");
Serial.print(bottomPhotoValue);
Serial.print(" top sensor = ");
Serial.print(topPhotoValue);
Serial.print(" dir pin = ");
Serial.print(directionVal);
Serial.print(" previous value = ");
Serial.println(previousValue);
#endif
} // End while (previousVal < 0)
directionVal = !directionVal;
} // End if (previousVal < 0)
}
/*******
This method rotates the steppermotor steps units, where a unit is
defined by the type of stepper motor used. For a NEMA 17, each step
corresponds to 1.8 degree, or 200 steps to make a single revolution.
The Pololu motor driver is capable of microsteps less that 1.8 degrees,
but this code does not make use of microstepping.
Parameters:
int steps the number of steps we wish to rotate the motor
Return value;
void
******/
void RotateStepper(int steps)
{
int i;
digitalWrite(ENABLEPIN, LOW); // LOW turns stepper driver on
delay(SETTLESTEPPER);
digitalWrite(STEPPIN, HIGH);
delay(SETTLESTEPPER);
}
}
/********
* This method reads the values from the two photoresistors that make up
* the sensor. It is assumed that the sensor is positioned correctly to
* the solar panel for reading.
Parameters:
void
Return value:
void
********/
void ReadSensors()
{
topPhotoValue = analogRead(TOPSENSORPIN); // Get values for top - bottom
// sensors
delayMicroseconds(READSENSORDELAY); // Prevent "digital jitters"
bottomPhotoValue = analogRead(BOTTOMSENSORPIN);
delayMicroseconds(READSENSORDELAY);
}
}
}
}
I checked the code you posted and it compiled without incident after I fixed some minor errors like there being a few extra close brackets } in some places and some typos and capitalization errors with your variables. Keep in mind that Arduino (like many other common programming languages) is case sensitive, so several of the issues were from errors like using directionVAL in some places and directionVal in others.
- Patrick
Thanks for checking it over. I will re-comb it for typos. I have written and compiled many other sketches. This code was direct from the “Examples.”
I didn’t think it would be “hosed”.
John
Which “Examples” are you referring to? Can you link to the specific page that led you to that program?
- Patrick
Hi Patrick,
My mistake about ‘Exapmles.’ I pulled the sketch from my ‘Sketchbook/ Solar Tracker’ in my IDE. It had the comment "Code based upon:
http://forum.pololu.com/viewtopic.php?f=15&t=6424" That is why I contacted you folks.
I have gone through the code to remove extra “}” and checked for incorrect spelling. It still won’t compile,
but I am working on it. I have done some “Sketch work” but not a pro by any means.
Thanks for your help.
John
Hi Patrick,
On my system, I have 2 profiles and more than 1 Arduino folder(different names). I was frustrated and confused myself.
I was working with a buggy file. I think there may have been some “Hidden” Ctrl-codes somewhere.
Is that possible? To have a Ctrl-char hidden due to fat fingering at sometime? I worked, many years ago with Data General
mini-computers and discovered that the command editor was like a pipe. Whatever went into the pipe came out in the same order
as entered. I guess I was sloppy looking at the Solar Tracker code.
Thanks for your help.
John
I am not entirely sure what you are asking, but in general it is hard for us to speculate about what specific file handling or modification errors might have occurred. For what it’s worth, as I mentioned before, I was able to compile the code after fixing the extra brackets, typos, and capitalization errors. In case it helps, I attached my version here, though please note that it is still not as organized as I would typically aim for; I just made the minimum modifications to make the program compile using Arduino:
/********
Code based upon:
******/
#include <Stepper.h>
#define DEBUG 1 // Used to toggle debug code
#define SETTLESTEPPER 10 // Time for stepper to change state
#define READSENSORDELAY 5 // Time for sensors to stabilize
#define TOPSENSORPIN A0 // Pin for top sensor
#define BOTTOMSENSORPIN A2 //Pin for bottom sensor
#define SLEEPPIN 11 // Sleep pin on driver
#define RESETPIN 12 // Reset pin on driver
#define STEPPIN 9 // Step pin on driver
#define DIRECTIONPIN 8 // Direction pin on driver
#define ENABLEPIN 10 // Enable pin on driver
#define SENSORSENSITIVITY 60 // Sensor difference that causes a rotation
#define STEPPERINCREMENT 10 // This many steps (18 degrees) to move panel
int directionVal =0;
int topPhotoValue =0; // Reading from top photoresistor
int bottomPhotoValue = 0; // Reading from bottom photoresistor
int previousValue = 0; // Previous reading from sensor
// =============================== setup90 ===============
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
#endif
pinMode(SLEEPPIN,OUTPUT); // Initialize stepper pin on uC
pinMode(RESETPIN,OUTPUT);
pinMode(DIRECTIONPIN,OUTPUT);
pinMode(STEPPIN,OUTPUT);
pinMode(ENABLEPIN, OUTPUT);
pinMode(TOPSENSORPIN, INPUT); //Set sensor pins
pinMode(BOTTOMSENSORPIN, INPUT);
digitalWrite(SLEEPPIN, HIGH); //Make sure defaults are set
digitalWrite(RESETPIN, HIGH);
}
// ======================= LOOP() ==========================
void loop()
{
void ReadSensors(); // Sensor read to get top and bottom
// sensor values
#ifdef DEBUG
Serial.print("bottom sensor = ");
Serial.print(bottomPhotoValue);
Serial.print(" top sensor = ");
Serial.print(topPhotoValue);
Serial.print(" dir pin = ");
Serial.print(directionVal);
Serial.print(" previous value = ");
Serial.println(previousValue);
#endif
if (abs(bottomPhotoValue - topPhotoValue) > SENSORSENSITIVITY)
{
RotateStepper(STEPPERINCREMENT);
delay(1000);
}
previousValue = topPhotoValue - bottomPhotoValue;
if (previousValue < 0) // Wrong direction?
{
directionVal = !directionVal;
while (previousValue < 0) {
RotateStepper(STEPPERINCREMENT);
ReadSensors();
previousValue = topPhotoValue - bottomPhotoValue;
#ifdef DEBUG
Serial.print("**************** bottom sensor = ");
Serial.print(bottomPhotoValue);
Serial.print(" top sensor = ");
Serial.print(topPhotoValue);
Serial.print(" dir pin = ");
Serial.print(directionVal);
Serial.print(" previous value = ");
Serial.println(previousValue);
#endif
} // End while (previousVal < 0)
directionVal = !directionVal;
} // End if (previousVal < 0)
}
/*******
This method rotates the steppermotor steps units, where a unit is
defined by the type of stepper motor used. For a NEMA 17, each step
corresponds to 1.8 degree, or 200 steps to make a single revolution.
The Pololu motor driver is capable of microsteps less that 1.8 degrees,
but this code does not make use of microstepping.
Parameters:
int steps the number of steps we wish to rotate the motor
Return value;
void
******/
void RotateStepper(int steps)
{
int i;
digitalWrite(ENABLEPIN, LOW); // LOW turns stepper driver on
delay(SETTLESTEPPER);
digitalWrite(STEPPIN, HIGH);
delay(SETTLESTEPPER);
}
/********
* This method reads the values from the two photoresistors that make up
* the sensor. It is assumed that the sensor is positioned correctly to
* the solar panel for reading.
Parameters:
void
Return value:
void
********/
void ReadSensors()
{
topPhotoValue = analogRead(TOPSENSORPIN); // Get values for top - bottom
// sensors
delayMicroseconds(READSENSORDELAY); // Prevent "digital jitters"
bottomPhotoValue = analogRead(BOTTOMSENSORPIN);
delayMicroseconds(READSENSORDELAY);
}
- Patrick
Hi! You are seeing this error because boolean is a non-standard legacy type specific to the Arduino environment. When you compile using a standard C++ compiler (which is often the case with Pololu’s libraries or non-Arduino IDEs), the compiler only recognizes the standard bool type.
To fix this, simply replace all instances of boolean with the standard bool type:
C++bool myFlag = false; // Use this instead of boolean
Also, double-check that you have #include <Arduino.h> at the top of your file if you are using an Arduino-compatible board, as that header usually handles the boolean typedef for backward compatibility.
For more details on standard types, you can check the C++ Reference for bool.
Thanks Oliver2003. That explains the issue.
73, John
