Hello guys!
I'm currently working on a simplified incubator using arduino nano(ATmega 328p) for its brain, 2pcs seven segment (tm1637) to display temperature and humidity level, 4pcs fotek ssr to control 50ktyz AC220V motor(cw, ccw), humidifier, heater, and 2pcs buttons to control motor position manually.
My codes are working perfectly fine, but whenever I add the buttons for manual control the fotek ssr suddenly wont work. This is the only problem I facing, I tried all my best but seems not enough.
const int relayleft = A3;
const int relayright = A2;
const int buttonleft = 6;
const int buttonright = 7;
const int limitleft = 11;
const int limitright = 12;
int relayleftstate = LOW;
int relayrightstate = LOW;
unsigned long relay_interval = 30000; //For testing only
unsigned long previousMillis = 0;
int control = 0;
int control_state = 0;
void setup() {
Serial.begin(9600);
pinMode(relayleft, OUTPUT);
pinMode(relayright, OUTPUT);
pinMode(buttonleft, INPUT_PULLUP);
pinMode(buttonright, INPUT_PULLUP);
pinMode(limitleft, INPUT_PULLUP);
pinMode(limitright, INPUT_PULLUP);
relayleftstate = LOW; //Keeping the relay off during booting up
digitalWrite(relayleft, LOW); //Keeping the relay off during booting up
relayrightstate = LOW; //Keeping the relay off during booting up
digitalWrite(relayright, LOW); //Keeping the relay off during booting up
}
void loop() {
int buttonleftstate = digitalRead(buttonleft);
int buttonrightstate = digitalRead(buttonright);
if (buttonleftstate == HIGH) {
relayleftstate = LOW;
digitalWrite(relayleft, LOW);
}
else if (buttonleftstate == LOW){
relayleftstate = HIGH;
digitalWrite(relayleft, HIGH);
control = 2;
}
if (buttonrightstate == HIGH) {
relayrightstate = LOW;
digitalWrite(relayright, LOW);
}
else if (buttonrightstate == LOW){
relayrightstate = HIGH;
digitalWrite(relayright, HIGH);
control = 4;
}
tiltcontrol();
}
void tiltcontrol(){
control_state = control;
int limitleftstate = digitalRead(limitleft);
int limitrightstate = digitalRead(limitright);
unsigned long currentMillis = millis();
switch(control){
case 0: //Start
Serial.println("START");
control = 1;
break;
case 1: //left
if ((relayleftstate==LOW) && (currentMillis-previousMillis>=relay_interval))
{
previousMillis = currentMillis;
relayleftstate = HIGH;
digitalWrite(relayleft, HIGH);
Serial.println("TURNING LEFT");
control = 2;
}
break;
case 2://left stop
if(limitleftstate==LOW) {
Serial.println("STOP LEFT");
relayleftstate = LOW;
digitalWrite(relayleft, LOW);
control = 3;
}
break;
case 3: //right
if((relayrightstate==LOW) && (currentMillis-previousMillis>=relay_interval))
{
previousMillis = currentMillis;
relayrightstate = HIGH;
digitalWrite(relayright, HIGH);
Serial.println("TURNING RIGHT");
control = 4;
}
break;
case 4: //right stop
if(limitrightstate == LOW) {
Serial.println("STOP RIGHT");
relayrightstate = LOW;
digitalWrite(relayright, LOW);
control = 0;
}
break;
}
}
How can I implement the buttons for manual control without interfering fotek ssr (motor control). The code I uploaded is only the motor control, the other code is working. Any suggestions and answers would be great help, thanks!