Hello There
First time poster, i have been lurking around for a couple of months to try and get a sketch working for my particular project. I have read several forums and researched coding but i just cant quite nail this.
Hardware: ATmega32U4 Micro
Nema 17 Stepper Motor
A4988 Stepper Drive
DS18b20 Temp Sensor
LCD 1602 Module
Project: I want to define a personal ideal ambient temperature, for example 25 Deg Cel. I then want to control the stepper motor to "if temp less than ideal ambient temp" rotate anticlock wise 2 steps, then wait say 5 mins then read temp if its higher or still lower than ideal temp rotate 2 steps anticlockwise again or if higher rotate 2 steps clockwise. And so on.
Ive acheived the easy parts with my limited knowledge by using sample codes but cant get the motor to "bounce" off the temperature.
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 10 on the Arduino
#define ONE_WIRE_BUS 10
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int Contrast=120;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int tempPin = 10; //arduino pin used for temperature sensor
int tempRight = 25; // the ideal temperature
int tempC = (sensors.getTempCByIndex(0));
const int stepPin = 7;
const int dirPin = 8;
void setup() {
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
analogWrite(6,Contrast);
lcd.begin(16,2);
pinMode (7,OUTPUT);
pinMode (8,OUTPUT);
}
void loop() {
lcd.setCursor(3,0);
lcd.print("ROOM TEMP");
lcd.setCursor(3,1);
lcd.print(sensors.getTempCByIndex(0));
lcd.setCursor(10,1);
lcd.print("Cel");
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
{
if(tempC < tempRight) // if temp is lower than ideal temp
digitalWrite(dirPin,HIGH);
for(int x = 0; x < 2; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
if(tempC > tempRight) // If temp is higher than ideal temp
digitalWrite(dirPin,HIGH);
for(int x = 0; x < 2; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}}}
The motor just goes in 2 steps in one direction everytime the temp is printed into the serial monitor ignoring the temp set points.
Any help or guidance would be very much appreciated thanks