I am relatively new to programming and the Arduino IDE, I am working on a project that is working now, but I would like to change it to better suit my needs. I really need another set of eyes and some suggestions as to my approach.
My project turns on a laser and a stepper motor at intervals of 2 min and 10 min, currently I am using one sketch for the 2 min and another for the 10 min. I would like to use only one sketch by using a switch to toggle a pin high/low to select the run interval time. I have the sketch written but am having trouble getting it to compile, I get an error “expected unqualified-id before ‘if’ ” and ‘else’. I would like to have someone with more experience than I, look at my sketch and point out my error.
Also, I would welcome any suggestions as to how to accomplish my desired effect with a different approach, if easier or better in some way.
Many thanks in advance,
elalto
SKETCH:
/*-----( Import needed libraries )-----*/
#include <Stepper.h>
#include <TimedAction.h>
void test();
void testo();
void myCode();
void on();
TimedAction firstAction = TimedAction((2*60*1000UL),test); //each 2 mns Call myCode
TimedAction secondAction = TimedAction((1.95*60*1000UL),testo); //each 1.95 mns Call fet
TimedAction hourCodeAction = TimedAction((10*60*1000UL),myCode); //each 10 mns Call myCode
TimedAction lsrOnAction = TimedAction((9.99*60*1000UL),on); //each 9.90 mns Call fet
int photocellPin = A1; // the cell and 10K pulldown are connected to
int photocellReading; // the analog reading from the sensor divider
int lsrPin = 10;
int buttonPin = 3; // Pins to connect to
int buttonPinReading; //the digital reading from the switch
#define STEPS_PER_MOTOR_REVOLUTION 32
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64 //2048
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 2, 4, 3, 5);
int Steps2Take;
void setup()
{
pinMode (buttonPin, INPUT_PULLUP); // PIN 3 is an INPUT
pinMode(photocellPin, INPUT);
pinMode(lsrPin, OUTPUT);
Serial.begin(9600);
}
/*--(end setup )---*/
void loop()
{
int buttonPinReading = digitalRead(buttonPin);
Serial.print("digital reading = ");
Serial.println(buttonPinReading);
photocellReading = analogRead(photocellPin);
photocellReading = 1023 - photocellReading;
Serial.print("Analog reading = ");
Serial.println(photocellReading);
}
if (digitalRead(buttonPin) == HIGH && photocellReading > 800)
{
firstAction.check();
secondAction.check();
}
void test()
{
digitalWrite(lsrPin, HIGH);
}
void testo()
{
digitalWrite(lsrPin, HIGH);
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION ; // Rotate CW 1 turn
small_stepper.setSpeed(30);
small_stepper.step(Steps2Take);
Steps2Take = - STEPS_PER_OUTPUT_REVOLUTION ; // Rotate CCW 1 turn
small_stepper.setSpeed(30);
small_stepper.step(Steps2Take);
digitalWrite(lsrPin, LOW);
}
else if (digitalRead(buttonPin) == LOW && photocellReading > 800)
{
lsrOnAction.check();
hourCodeAction.check();
}
void on()
{
digitalWrite(lsrPin, HIGH);
}
void myCode()
{
digitalWrite(lsrPin, HIGH);
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION ;
small_stepper.setSpeed(30);
small_stepper.step(Steps2Take);
Steps2Take = - STEPS_PER_OUTPUT_REVOLUTION ; // Rotate CCW 1 turn
small_stepper.setSpeed(30);
small_stepper.step(Steps2Take);
digitalWrite(lsrPin, LOW);
}
Moderatore: please use [code] and [/code] tags for code
Move all the # lines to the top of the sketch.
Make sure all variables are declared as a data type: byte, int, long, unsigned long, float, etc.
Same for arrays, must be a data type.
Fix those, and report back. I can't compile where I am at now.
In addition, you can't create a function eg void test() inside another function.
You might get a better idea how to organize your code from the Thread planning and implementing a program. It also shows how to use millis() to manage timing. I don't know anything about the TimeAction library.
/*-----( Import needed libraries )-----*/
#include <Stepper.h>
#include <TimedAction.h>
//void test();
void testo();
void myCode();
void on();
TimedAction secondAction = TimedAction((1.95*60*1000UL),testo); //each 1.95 mns Call fet
TimedAction hourCodeAction = TimedAction((10*60*1000UL),myCode); //each 10 mns Call myCode
TimedAction lsrOnAction = TimedAction((9.99*60*1000UL),on); //each 9.90 mns Call fet
int photocellPin = A1; // the cell and 10K pulldown are connected to
int photocellReading; // the analog reading from the sensor divider
int lsrPin = 10;
int buttonPin = 3; // Pins to connect to
int buttonPinReading; //the digital reading from the switch
#define STEPS_PER_MOTOR_REVOLUTION 32
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64 //2048
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 2, 4, 3, 5);
int Steps2Take;
void setup()
{
pinMode (buttonPin, INPUT_PULLUP); // PIN 3 is an INPUT
pinMode(photocellPin, INPUT);
pinMode(lsrPin, OUTPUT);
Serial.begin(9600);
}
/*--(end setup )---*/
void loop()
{
int buttonPinReading = digitalRead(buttonPin);
Serial.print("digital reading = ");
Serial.println(buttonPinReading);
photocellReading = analogRead(photocellPin);
photocellReading = 1023 - photocellReading;
Serial.print("Analog reading = ");
Serial.println(photocellReading);
if ( digitalRead(buttonPin) == HIGH && photocellReading > 800 )
{
// firstAction.check();
secondAction.check();
}
}
void testo()
{
digitalWrite(lsrPin, HIGH);
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION ; // Rotate CW 1 turn
small_stepper.setSpeed(30);
small_stepper.step(Steps2Take);
Steps2Take = - STEPS_PER_OUTPUT_REVOLUTION ; // Rotate CCW 1 turn
small_stepper.setSpeed(30);
small_stepper.step(Steps2Take);
digitalWrite(lsrPin,LOW);
if ( digitalRead(buttonPin) == LOW && photocellReading > 800)
{
lsrOnAction.check();
hourCodeAction.check();
}
}
void on()
{
digitalWrite(lsrPin, HIGH);
}
void myCode()
{
digitalWrite(lsrPin, HIGH);
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION ;
small_stepper.setSpeed(30);
small_stepper.step(Steps2Take);
Steps2Take = - STEPS_PER_OUTPUT_REVOLUTION ; // Rotate CCW 1 turn
small_stepper.setSpeed(30);
small_stepper.step(Steps2Take);
digitalWrite(lsrPin, LOW);
}
Note how "Void Test();" has been commented out along with the line:
"TimedAction firstAction = TimedAction((2601000UL),test); //each 2 mns Call myCode" which calls
Test. See Robin's comment above.
FYI, I had to replace "WProgram.h" with "Arduino.h" in the TimedAction.h file.
Ask why.
ALSO,
You can't have this:
if (digitalRead(buttonPin) == HIGH && photocellReading > 800)
{
firstAction.check();
secondAction.check();
}
IN BETWEEN TWO FUNCTIONS OUTSIDE void loop().
I moved it into the function directly above it.
Someone else can probably explain this better than I .
I still don't understand the "function inside a function" comment, but I will keep reading and I am sure that I will "get it" yet.
The problem with timedAction is that it was written long ago and arduino.h was not in there, I replaced it before I started and it works fine, sorry I did not mention this in my post, it was a long time ago.
I will study your modifications to my sketch, I don't know if the sketch will do what I want but at least it compiles now and I can try it, thanks again.
OK, I uploaded the sketch and found that the first "if" seems to work fine, however, the second "if" does not.
The program should light the laser and run the motor every 2min when the buttonPin is HIGH if the buttonPin is LOW it should run every 10min. How can I do a "else if", I've tried and get an error.
EDITEDITEDITEDIT*EDIT
NEVER MIND, I figured it out and it works excellent.