Im working on an Arduino controlled motorised time-lapse dolly and I'm trying to make the code as simple as possible. So far i can see what i wrote working well but i have two problems.
To start, this is part of the code:
while(stage1 == 1023 && stage2 == 0)
{
int shutterIntervalVal = analogRead(knob);
int shutterInterval = map(shutterIntervalVal,0,1023,0,120);
shutterInterval = int(shutterInterval +0.5);
lcd.setCursor(0,0);
lcd.print("Set interval");
delay(500);
lcd.setCursor(0,1);
lcd.print(shutterInterval);
Serial.println(shutterInterval);
delay(1);
if(confirmBut == 1023){
int const offShutterInterval = shutterInterval;
lcd.clear();}
stage1 and stage2 are two switches in a 3-way-switch connected to analog pins(i was running out of digital pins)
Pretty simple.. And when uploaded alone works perfectly.
Yet, when i upload it with the other 2 stages which are almost the same way written with different conditions, the Arduino starts mixing up the 3 stages and running them crazy!
So how can i make the Arduino split them up to 3 different stages according to the change in conditions.
Another concern i have is for when the confirm button is pressed i need the arduino to store the value at that instant because i will use it in the third stage. So does it stay stored or should i use an alternative way?
Thankyou for your time
FULL CODE:
#include <LiquidCrystal.h>
#include <Stepper.h>
#include <math.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int const confirmBut = A3; // Confirm Button
int const stage1 = A1; // 1st stage on switch
int const stage2 = A2; // second stage on switch
int const stepsPerRevolution = 200;
Stepper motor(stepsPerRevolution, 7, 8, 9, 10);
int stepCount = 0;
int const knob = A0;
int shutterInterval = 0;
int offShutterInterval = 0;
int offShutterSpeed = 0;
void setup() {
lcd.begin(16,2);
lcd.print("Welcome Mohamad");
delay(1000);
lcd.clear();
Serial.begin(9600);
motor.setSpeed(60);
}
void loop() {
Serial.println(analogRead(A1));
if(stage1 == 1023 ||stage1 == 10 && stage2 < 1023)
{
int shutterIntervalVal = analogRead(knob);
int shutterInterval = map(shutterIntervalVal,0,1023,0,120);
shutterInterval = int(shutterInterval +0.5);
lcd.setCursor(0,0);
lcd.print("Set interval");
delay(500);
lcd.setCursor(0,1);
lcd.print(shutterInterval);
Serial.println(shutterInterval);
delay(1);
if(confirmBut == 1023){ int const offShutterInterval = shutterInterval;
lcd.clear();}
}
else if(stage1 < 1023 && stage2 < 1023)
{
int shutterSpeedVal = analogRead(knob);
int shutterSpeed = map(shutterSpeedVal,0,1023,0,10);
shutterSpeed = int(shutterSpeed +0.5);
lcd.setCursor(0,0);
lcd.print("Set shutter speed");
delay(500);
lcd.setCursor(0,1);
lcd.print(shutterSpeed);
Serial.println(shutterSpeed);
delay(1);
if(confirmBut == 1023){ int offShutterSpeed = shutterSpeed; lcd.clear();}
}
else if(stage1 < 1023 && stage2 == 1023)
{ int convShutterInterval = map(offShutterInterval,0,120,0,200);
convShutterInterval = int(convShutterInterval +0.5);
motor.step(stepsPerRevolution);
delay(offShutterSpeed + 100);}
}
I realised further more problems, i added serial print to the read on A1 which on closed switch should give a value of 1023 . But even when sometimes it gives 1023 the arduino skips and remains in stage 2. And also the number 10 keeps popping out in all the prints...
I need urgent help