PLEASE HELP! There at some issues in this code and I have a project due soon, so it would be much appreciate if anyone that knew how to fix them could help me. Thank You
/*
Arduino with PIR motion sensor
activate camera motor projects
Writes by Engineer Mohanad Hameed */ #include <Stepper.h> int led = 13; // the pin that the LED is attached to
int sensor = 2; // the pin that the sensor is attached to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
int in1Pin = 12; //the pin that the phase 1 is attached to
int in2Pin = 11; //the pin that the phase 2 is attached to
int in3Pin = 10; //the pin that the phase 3 is attached to
int in4Pin = 9; //the pin that the phase 4 is attached to
int step_num =700;
Stepper motor(64, in1Pin, in2Pin, in3Pin, in4Pin);
void setup() {
pinMode(sensor, INPUT); // initialize sensor as an input
pinMode(in1Pin, OUTPUT); // initialize in1pin as an output
pinMode(in2Pin, OUTPUT); // initialize in2pin as an output
pinMode(in3Pin, OUTPUT); // initialize in3pin as an output
pinMode(in4Pin, OUTPUT); // initialize in4pin as an output
motor.setSpeed(300); //speed of the motor
void loop() {
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
motor.step(step_num); //rotate the motor foreword
delay(1000); // delay 300 milliseconds
motor.step(-step_num); //rotate the motor backward
delay(300);
if (state == LOW)
{ state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
motor.step(0);
if (state == HIGH) {
state = LOW; // update variable state to LOW
}
}
}
Was just missing a } to close setup(), and then CTRL-T to make the formatting easier to read. Many of the ints could be bytes, they do not exceed 255.
/*
Arduino with PIR motion sensor
activate camera motor projects
Writes by Engineer Mohanad Hameed */
#include <Stepper.h>
int led = 13; // the pin that the LED is attached to
int sensor = 2; // the pin that the sensor is attached to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
int in1Pin = 12; //the pin that the phase 1 is attached to
int in2Pin = 11; //the pin that the phase 2 is attached to
int in3Pin = 10; //the pin that the phase 3 is attached to
int in4Pin = 9; //the pin that the phase 4 is attached to
int step_num =700;
Stepper motor(64, in1Pin, in2Pin, in3Pin, in4Pin);
void setup() {
pinMode(sensor, INPUT); // initialize sensor as an input
pinMode(in1Pin, OUTPUT); // initialize in1pin as an output
pinMode(in2Pin, OUTPUT); // initialize in2pin as an output
pinMode(in3Pin, OUTPUT); // initialize in3pin as an output
pinMode(in4Pin, OUTPUT); // initialize in4pin as an output
motor.setSpeed(300); //speed of the motor
} // << this brace was missing
void loop() {
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
motor.step(step_num); //rotate the motor foreword
delay(1000); // delay 300 milliseconds
motor.step(-step_num); //rotate the motor backward
delay(300);
if (state == LOW)
{
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
motor.step(0);
if (state == HIGH) {
state = LOW; // update variable state to LOW
}
}
}
Was just missing a } to close setup(), and then CTRL-T to make the formatting easier to read:
/*
Arduino with PIR motion sensor
activate camera motor projects
Writes by Engineer Mohanad Hameed */
#include <Stepper.h> int led = 13; // the pin that the LED is attached to
int sensor = 2; // the pin that the sensor is attached to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
int in1Pin = 12; //the pin that the phase 1 is attached to
int in2Pin = 11; //the pin that the phase 2 is attached to
int in3Pin = 10; //the pin that the phase 3 is attached to
int in4Pin = 9; //the pin that the phase 4 is attached to
int step_num =700;
byte led = 13; // and you need this, or similar
Stepper motor(64, in1Pin, in2Pin, in3Pin, in4Pin);
void setup() {
pinMode(sensor, INPUT); // initialize sensor as an input
pinMode(in1Pin, OUTPUT); // initialize in1pin as an output
pinMode(in2Pin, OUTPUT); // initialize in2pin as an output
pinMode(in3Pin, OUTPUT); // initialize in3pin as an output
pinMode(in4Pin, OUTPUT); // initialize in4pin as an output
motor.setSpeed(300); //speed of the motor
} // << this brace was missing
void loop() {
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
motor.step(step_num); //rotate the motor foreword
delay(1000); // delay 300 milliseconds
motor.step(-step_num); //rotate the motor backward
delay(300);
if (state == LOW)
{
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
motor.step(0);
if (state == HIGH) {
state = LOW; // update variable state to LOW
}
}
}
It means there is a hidden character somewhere in your sketch. Please attach the sketch file that's causing that issue and I'll fix it for you. You can attach a file by clicking the "Reply" button, then "Attachments and other options".
Here it is. I actually did have an ulterior motive. I've been wanting to get my hands on one of these sort of problem sketches so I can verify the correct way to instruct people how to fix them. Yours is already fixed but here's the instructions anyway for anyone else who might find this post after encountering the issue:
Open the sketch in the Arduino IDE
Edit > Select All
Edit > Copy
Open any thread on the Arduino forum
Click on the text area of the "Quick Reply"
Edit > Paste
Edit > Select All
Edit > Copy
Open the sketch in the Arduino IDE
Edit > Select All
Edit > Paste
File > Save
The reason this works is the hidden characters that cause the error are converted to spaces when you paste the sketch to the forum, then when you paste the fixed sketch back to the Arduino IDE the problem is solved. If you want to see where all the hidden characters are in the original sketch do Tools > Fix encoding and reload. There are other ways to fix the hidden characters but this solution is fairly simple and should work for anyone.
Is that when you compile the Sciencefair.ino file I attached to my last reply?
MagTheStupidCoder:
something about an invalid library
Please copy and paste the full warning message about the library. That's unrelated to the problem with your sketch but I'll try to help with that issue also.
The problem is Sciencefair isn't a library, it's a sketch. You can only put valid libraries in the C:\Users\Name\Documents\Arduino\libraries folder. Please move C:\Users\Name\Documents\Arduino\libraries\Sciencefair to C:\Users\Name\Documents\Arduino\Sciencefair or any other convenient location on your computer.
You're going to have to do better than that. Read what I wrote carefully. Try to follow my instructions. If there's something you still don't understand then take the time to fully explain and I'll try to help.