Help with temperature sensor to control a stepper motor

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.

Weedpharma

  if
   (temperatuerC (19.0>= myStepper.step stepsPerRevolution));
   else
   (tempertuerC (19< myStepper.step -stepsPerRevolution));

You are right. There is a problem here.
The code structure should be

if (something is true)
  {
    do something
  }
  else
  {
    do something else
  }

I apologise for not putting the code in a box :-[

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);
  
  
}

If someone could help explain where I have gone wrong and what I could do to get it right would be much appreciated.

See my previous reply for one obvious problem area and the solution.

Have I not done this then

if
temperature C 19> (is something is true)

myStepper.step stepsPerRevolution (do something)

else
myStepper.step -stepsPerRevolution (do something else)

Have I not done this then

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.

Thanks UkHelliBob, I changed my code to this:

if
   (temperatuerC 19.0>=){ myStepper.step stepsPerRevolution;}
   else
    {myStepper.step -stepsPerRevolution;}
  delay(500);
}

When verified error said "expected')' before numeric constant"

feeling stupid or to old for all this lol

Ps can I put {} inside {} ??

if (temperatuerC 19.0>=)

Which 2 values are being compared ? Why are the comparison operators after the 2 values ?

can I put {} inside {} ??

Yes. The pair of braces encloses a block of code and a block of code can enclose another block of code.

so is the code I am using not saying:

if temperature from the sensor is 19 or above

Are the 2 values: tempertureC
19 and above

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);

Good luck

if (temperatuerC >= 19.0)
{
  //do this
}
else
{
  //do something else
}

Now you fill in what should happen in each case.

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.

Thank you :slight_smile:

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()

phantombrit:
Thanks UkHelliBob, I changed my code to this:

if

(temperatuerC 19.0>=){ myStepper.step stepsPerRevolution;}
  else
   {myStepper.step -stepsPerRevolution;}
 delay(500);
}






When verified error said "expected')' before numeric constant"

feeling stupid or to old for all this lol 

Ps can I put {} inside {} ??

Is this the only change from the original code u upload at the begnning.
I am just new to the coding & all.

Should

myStepper.step stepsPerRevolution;

be

myStepper.step(stepsPerRevolution);

I am not familiar with the library so could be wrong.