Hi, I'm doing this project where I have to rotate the servo motor depending on a reading from an LDR and and rotate a stepper motor depending on how much time has elapsed - pretty straight forward. I worked out the codes for the different function separately and it worked as expected but when I put them together as one code - havoc!!
The problem was with the servo code I had to use roughly around 1500 delay, which throws of the time elapsed for the stepper motor code, and also I used some delay in the stepper code too. (stepper motor moves after every 6s, originally I had delay in the stepper motor code too which I scraped).
Anyway trying to fix the problem as it is I fiddled with the stepper motor code by looking at the Serial.print of time elapsed and counting 6 seconds and modifying the if conditions. The code looks horrible- it does work, but I would like to know a better way of executing the code, and also whats the use of making a function separately and calling it the loop-what advantages are there.
Here is the final code:-
#include <Servo.h>
Servo myServo;
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
#include <elapsedMillis.h>
elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs
#include <Stepper.h>
float stepsPerRevolution = 63.683;
Stepper small_stepper(stepsPerRevolution, 8, 10, 9, 11);
const int sensorPin = A0;
const int sensorPin2 = A1;
//variables:
int sensorVal = 0;
int sensorVal2 = 0;
int sensorMin = 1023;
int sensorMax = 0;
int angle;
int angle2;
int ang = 0;
void setup() {
Serial.begin(9600);
myServo.attach(5);
small_stepper.setSpeed(200);
lcd.begin (20, 4);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print("Tilt angle:");
lcd.setCursor(0, 1);
lcd.print("Azimuth: ");
while (millis() < 5000) {
sensorVal = analogRead(sensorPin);
// record the maximum sensor value
if (sensorVal > sensorMax) {
sensorMax = sensorVal;
}
// record the minimum sensor value
if (sensorVal < sensorMin) {
sensorMin = sensorVal;
}
}
Serial.print("MAX: ");
Serial.print(sensorMax);
Serial.print(", MIN: ");
Serial.print(sensorMin);
}
void loop() {
int sensorVal = analogRead(sensorPin);
int sensorVal2 = analogRead(sensorPin2);
//Serial.print(timeElapsed);
if (sensorVal > sensorVal2) {
angle = map(sensorVal, sensorMin, sensorMax, 0, 89);
angle = constrain(angle, 1, 90);
myServo.write(angle);
ang = angle;
// Serial.print("ONE: ");
// Serial.println(angle);
delay(15);
} else if (sensorVal2 > sensorVal) {
angle2 = map(sensorVal2, sensorMin, sensorMax, 180, 90);
angle2 = constrain(angle2, 90, 180);
myServo.write(angle2);
ang = angle2;
// Serial.print("TWO: ");
// Serial.println(angle2);
delay(15);
}
lcd.setCursor(11, 0);
if (ang < 10) lcd.print(" "); // 0-9 add one space
if (ang < 100) lcd.print(" "); // 0-99 add one space
if (ang < 1000) lcd.print(" "); // 0-999 add one space
lcd.print(ang);
lcd.print((char)223);
delay(1000);
Serial.println(timeElapsed);
if (timeElapsed > 0 && timeElapsed < 12213) {
lcd.setCursor(8, 1);
lcd.print(" 0");
lcd.print((char)223);
lcd.setCursor(0, 2);
lcd.print("January ");
}
if (timeElapsed > 12213 && timeElapsed < 13219) {
small_stepper.step(63.683);
}
if (timeElapsed > 13213 && timeElapsed < 18435) {
small_stepper.step(0);
lcd.setCursor(8, 1);
lcd.print(" 5");
lcd.print((char)223);
lcd.setCursor(0, 2);
lcd.print("February ");
}
if (timeElapsed > 18435 && timeElapsed < 19436 ) {
small_stepper.step(63.683);
}
if (timeElapsed > 19436 && timeElapsed < 24642) {
small_stepper.step(0);
lcd.setCursor(8, 1);
lcd.print("10");
lcd.print((char)223);
lcd.setCursor(0, 2);
lcd.print("March ");
} if (timeElapsed > 24642 && timeElapsed < 25642) {
small_stepper.step(63.683);
} if (timeElapsed > 25462 && timeElapsed < 30850) {
small_stepper.step(0);
lcd.setCursor(8, 1);
lcd.print("15");
lcd.print((char)223);
lcd.setCursor(0, 2);
lcd.print("April ");
}
if (timeElapsed > 30850 && timeElapsed < 31850) {
small_stepper.step(-63.683);
}
if (timeElapsed > 31850 && timeElapsed < 38237) {
small_stepper.step(0);
lcd.setCursor(8, 1);
lcd.print("10");
lcd.print((char)223);
lcd.setCursor(0, 2);
lcd.print("May ");
}
if (timeElapsed > 38237 && timeElapsed < 39237) {
small_stepper.step(-63.683);
}
if (timeElapsed > 39237 && timeElapsed < 45625) {
small_stepper.step(0);
lcd.setCursor(8, 1);
lcd.print(" 5");
lcd.print((char)223);
lcd.setCursor(0, 2);
lcd.print("June ");
}
if (timeElapsed > 45625 && timeElapsed < 46625) {
small_stepper.step(-63.683);
}
if (timeElapsed > 46625 && timeElapsed < 53013) {
small_stepper.step(0);
lcd.setCursor(8, 1);
lcd.print(" 0");
lcd.print((char)223);
lcd.setCursor(0, 2);
lcd.print("July ");
}
if (timeElapsed > 53013) {
timeElapsed = 7055;
}
}