This is my first coding project. I have connected a stepper motor and a temperature sensor to an UNO, and put together two codes from other examples and I have written my own code tacked on the end using "IF" and "else" obviously the errors are in my code, the last 2 lines.
The intention is to get the stepper to rotate in one direction above 19 degrees C and the other direction below 19 degrees.
#include <Stepper.h>
const int stepsPerRevolution = 48; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
int sensorPin = 0;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(150);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage/= 1024.0;
float temperatuerC = (voltage - 0.5) * 100;
Serial.print(temperatuerC); Serial.println("C");
// step one revolution in one direction:
if
(temperatuerC (19.0>= myStepper.step stepsPerRevolution));
else
(tempertuerC (19< myStepper.step -stepsPerRevolution));
delay(500);
You need to learn how to put your code in its own window as seen in other posts. This can be done by placing [code] and [/code] around the code. This makes it easier for others to read.
Have you tried compiling this code?
What happened?
What sensor and motor are you using?
Give us the information and ask a question rather than make a statement and expect us to work out what you want.
I'm not having a problem with the stepper motor (Mitsumi M35SP-9) or the temperature sensor (LM35). The error's are at my codes so I was just after a bit of help with that. If someone could help explain where I have gone wrong and what I could do to get it right would be much appreciated.
/*
Stepper Motor Control - one revolution
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe
*/
#include <Stepper.h>
const int stepsPerRevolution = 48; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
int sensorPin = 0;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(150);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage/= 1024.0;
float temperatuerC = (voltage - 0.5) * 100;
Serial.print(temperatuerC); Serial.println("C");
// step one revolution in one direction:
if
(temperatuerC (19.0>= myStepper.step stepsPerRevolution));
else
(tempertuerC (19< myStepper.step -stepsPerRevolution));
delay(500);
}
No. You need brackets round the test(s) and ideally braces round the actions. If the action is only one line of code you can technically leave out the braces but it is easier to follow what is going on if they are always used and if the code is Auto Formatted so that it is correctly indented.
Follow my layout. Put the test in the brackets (only one test is needed) and put the actions in the braces.
Hi, it looks like your is just all in the wrong order and just needs sorting out a little, but i am also just starting out with the Arduino so stand to be corrected but try this.
void loop() {
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage/= 1024.0;
float temperatuerC = (voltage - 0.5) * 100;
Serial.print(temperatuerC); Serial.println("C");
// step one revolution in one direction:
if (temperatuerC >=19) myStepper.step (stepsPerRevolution);
else if
(temperatuerC <19) myStepper.step (-stepsPerRevolution);
delay(500);
hi, can i ask a few questions with the code. On the if statement on the last part, from what i thought, the motor will rotate when the temp. rises to 19 degrees C and greater on the first loop, but what will happen in the second loop if the temp is still 19 C degrees, will it still run over again??? did i use the word loop right? sorry if i am wrong.
EverardEE_777:
hi, can i ask a few questions with the code. On the if statement on the last part, from what i thought, the motor will rotate when the temp. rises to 19 degrees C and greater on the first loop, but what will happen in the second loop if the temp is still 19 C degrees, will it still run over again??? did i use the word loop right? sorry if i am wrong.
If I understand correctly, your question is, how does loop() work? Well... in principle you should be starting a new thread. Luckily I know what code you refer to, even though you didn't say. However your question has a simple and obvious answer. Consult the documentation here.
On the if statement on the last part, from what i thought, the motor will rotate when the temp. rises to 19 degrees C and greater on the first loop, but what will happen in the second loop if the temp is still 19 C degrees, will it still run over again???
What you say is correct. If next time through the loop() function the temperature is above the threshold the conditional code will run again.
Whether or not that matters depends on what you want it to do. For instance if all it does is turn an LED on or off it won't matter if it is repeated but if it must only happen once then you can set a variable to true to flag that it has run and test that variable next time round to test whether it has previousy run. If so, don't do it again.
the motor will rotate when the temp. rises to 19 degrees C and greater on the first loop, but what will happen in the second loop if the temp is still 19 C degrees, from this, can it be modified so that the motor will rotate to a certain steps when the temperature is high and stops while the temperature is still high, and then it will rotate in the opposite direction with the same number of steps when the temperature is low and stops while the temperature is low.
can it be modified so that the motor will rotate to a certain steps when the temperature is high and stops while the temperature is still high, and then it will rotate in the opposite direction with the same number of steps when the temperature is low and stops while the temperature is low.
All of that is possible. One method is, as I suggested, to set a flag variable to indicate that the temperature is high but that the motor has previously moved.
Something like this pseudo code
start of loop()
if temperature is higher than threshold and highTempFlag is false
rotate motor to high position
set highTempFlag to true
set lowTempFlag to false
end if
else
if temperature is lower than threshold and lowTempFlag is false
rotate motor to low position
set lowTempFlag to true
set highTempFlag to false
end else
end of loop()