Hi all,
So I have two pumps that I control with a motor driver and 14 solenoid valves that I'm controlling using 7 2-relay modules.
I have a function called withdraw, basically, this function opens up all valves and both pumps in the reverse direction, but once i send the command to switch off everything, the Arduino restarts just as if i pressed the reset button.
I'm powering Arduino through the Vin pin using a 5 volts source
I cannot understand the cause of this, according to google usually this issue is coming from incorrect wiring, but I'm sure of my wiring and also when i control 4 valves only or say 10 valves only, i don't have any problem
The problem comes only when i control the two pumps and the 14 valves all together.
EDIT: i added the Serial.println functions to see where is the problem exactly, the Serial print get the I go A and the I go to B shown in the code, but the arduino restarts before the C and D show off
Here is the code:
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {23, 25, 27, 29};
byte colPins[COLS] = {31, 33, 35, 37};
Keypad kpd = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const int waterpumpA = 3; // the pump connected to the water tank
const int waterpumpB = 2; // the pump connected to the water tank
const int methpumpA = 4; // the pump connected to the other tank
const int methpumpB = 5; // the pump connected to the other tank
const int wvalve[] = {19, 18, 17, 16, 15, A10, 13}; // valves connected to tank1
const int mvalve[] = {12, 11, 10, 9, 8, 7, 6}; // valves connected to tank 2
uint8_t state; // state machine
const uint8_t mainmenue = 1;
const uint8_t firstmainmenue = 2;
const uint8_t stoppingofnot = 3;
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
state = firstmainmenue;
Serial.begin(9600);
for (int i = 0; i < 7; i++)
{
Serial.print("we are at setup now and i= ");
Serial.println(i);
Serial.println(wvalve[i]);
pinMode(wvalve[i], OUTPUT);
pinMode(mvalve[i], OUTPUT);
digitalWrite(wvalve[i], HIGH); //high means closed for this valve
digitalWrite(mvalve[i], HIGH);
}
pinMode(waterpumpA, OUTPUT);
pinMode(methpumpA, OUTPUT);
pinMode(waterpumpB, OUTPUT);
pinMode(methpumpB, OUTPUT);
}
void loop() {
if (state == firstmainmenue)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press C to start");
state = mainmenue;
}
///////////////////////////////////////////////////
//////////////////////////////////////////////////
else if (state == mainmenue)
{
//Serial.println("Main menue");
char key = kpd.getKey();
if (key)
{
Serial.println(key);
if (key == 'C')
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Withdrawing..");
lcd.setCursor(0, 1);
lcd.print("press # to stop");
for (int i = 0; i < 7; i++)
{
digitalWrite(wvalve[i], LOW); //opens the valves
digitalWrite(mvalve[i], LOW); // opens the valves
}
// digitalWrite(waterpumpB, HIGH); //switch on the pump 1 in the reverse direction
// digitalWrite(methpumpB, HIGH);//switch on the pump 2 in the reverse direction
state = stoppingofnot;
}
}
}
///////////////////////////////////////////////////
//////////////////////////////////////////////////
else if (state == stoppingofnot)
{
char key3 = kpd.getKey();
if (key3)
{
if (key3 == '#') //my problem us here, this works actually and they all switch off but then the arduino crashes as soon as everything is off
{
Serial.println( " I got to A"); //this one works
pumpstop();
Serial.println( " I got to B"); //works!!
delay(100);
valvestopall();
Serial.println( " I got to C"); //never printed
delay(100);
state = firstmainmenue;
Serial.println( " I got to D"); //never printed
}
}
}
}
///////////////////////////////////////////////////
//////////////////////////////////////////////////
///////////////////////////////////////////////////
//////////////////////////////////////////////////
void valvestopall()
{
for (int i = 0; i < 7; i++)
{
Serial.print("We are at closing loop and the vlaue of i= "); //just to see if the whole loop is being excuted or not
Serial.println(i); //all of them were printed
Serial.println(wvalve[i]);
digitalWrite(wvalve[i], HIGH);
delay(100);
digitalWrite(mvalve[i], HIGH);
delay(100);
}
}
///////////////////////////////////////////////////
//////////////////////////////////////////////////
///////////////////////////////////////////////////
//////////////////////////////////////////////////
void pumpstop()
{
digitalWrite(waterpumpA, LOW);
delay(100);
digitalWrite(waterpumpB, LOW);
delay(100);
digitalWrite(methpumpA, LOW);
delay(100);
digitalWrite(methpumpB, LOW);
delay(100);
}
