Sorry in advance for any silly questions here. This is my first project. I am trying to use a stepper motor to move a pointer for a thermometer.
The stepper motor is a 28BYJ-48 and is being driven by a ULN2003 driver board. There is a small pointer attached to the motor shaft.
I think the project will be broken out into 2 main parts. Setting up the temp sensor, and using that data to move the motor to the correct position. I'm waiting on parts for my temp sensor, so I'm trying to work out the motor part while manually entering temperature readings into the serial monitor. Eventually I plan on having the temperature read every 5 min or so and the serial monitor part will go away.
I have a switch to home the motor on startup. That part of the sketch seems to work OK. Power on and the motor turns back to zero and closes the switch, then backs off slowly until the switch opens again. That works OK.
From there, I want to read a temp (say 70 deg) and have the motor move the pointer to that position. That parts seems to work as well. But I need a way so that when the temp is read again, it doesn't just add another 70 deg (or whatever the temp is) to the existing position. So I created some variables called "current_temp", "new_temp", and "temp_change". My thinking was:
new_temp-current_temp=temp_change
then use that temp-change value to calculate how many steps to move the motor. Then I would need to take the "new_temp" value and store it as the "current_temp" value before doing the whole loop again. But when I tried to set that up in the sketch...it doesn't work. I think I'm doing something very basically wrong.
//Libraries:
#include <Stepper.h>
//Hardware Declarations:
#define home_switch 2
//Stepper Declarations:
const int revsteps = 2051; // change this to fit the number of steps per revolution
const int RPM = 10; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
Stepper stepper1(revsteps, 8, 10, 9, 11);
int steps;
//Temp Declarations:
int current_temp;
int new_temp;
int temp_change;
void setup() {
pinMode(home_switch, INPUT_PULLUP);
stepper1.setSpeed(5);
Serial.begin(9600);
Serial.println("-----HOMING-----");
//-----HOME STEPPER-----
while(digitalRead(home_switch)){
stepper1.step(-1);
delay(10);
}
while(!digitalRead(home_switch)){
stepper1.step(1);
delay(10);
}
current_temp=5;
stepper1.setSpeed(RPM);
Serial.println("<<Waiting for Temp>>");
}
void loop() {
//later replace this function with actual temp sensor input
if (Serial.available())
{
new_temp=Serial.parseInt();
temp_change=new_temp-current_temp;
steps=map(temp_change,0,100, 0, 630); //Converting temp to number of steps
stepper1.step(steps); //Move stepper.
}
stepperpoweroff();
new_temp=current_temp;
}
void stepperpoweroff() {
digitalWrite(8, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
}
I noticed that the stepper would get very hot when sitting holding position. The pointer I'm using is very light and the motor wouldn't move while waiting for the next cylce, so I have a function to power off the motor after each movement.
Another odd thing...my thermometer doesn't home to 0 degrees...when homed it goes to 5 deg. It's an odd thing...but you can see that's what I'm trying to do after the homing sequence with "current_temp=5;" That also doesn't seem to work.
I think I'm probably making some silly beginner mistake(s) here. Anyone have any ideas?