Hi,
I want to run a motor for a certain amount of time when the run switch is activated. I took the concept of the blink without delay but the problem is that the motor runs as long as the run switch is activated and not for the certified amount of time. When I look in the serial monitor the difference between the currentMillis and previousMillis is 0 and I believe this is the reason that the motor doesn't stop running after the certified time has passed.
I would very much appreciate if you could help me solve this problem.
Regards,
#include <SPI.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
int value=0;
int cementWeight = 0;
int sandWeight = 0;
int waterWeight = 0;
char selection;
int page_counter=1;
const int previous = 52;
const int next = 50;
const int runSwitch = 53;
const int mixerSwitch = 47;
const int conveyorSwitch = 46;
const int augerSwitch = 48;
unsigned long previousMillis = 0;
unsigned long interval = 50000;
boolean current_previous = LOW;
boolean last_previous=LOW;
boolean last_next = LOW;
boolean current_next = LOW;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {14,15,16,17}; //row pinouts of the keypad (L1, L2, L3, L4)
byte colPins[COLS] = {18,19,20,21}; //column pinouts of the keypad (R1, R2, R3)
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd1(2,3,4,5,6,7);
LiquidCrystal lcd2(2,9,4,5,6,7);
void setup()
{
Serial.begin(9600);
lcd1.begin(20, 4);
lcd2.begin(20, 4);
lcd1.clear();
lcd2.clear();
pinMode(previous, INPUT);
pinMode(next, INPUT);
pinMode(runSwitch, INPUT);
pinMode(mixerSwitch, OUTPUT);
pinMode(conveyorSwitch, OUTPUT);
pinMode(augerSwitch, OUTPUT);
displayTopMenu();
displayLiveWeight();
}
boolean debounce(boolean last, int pin)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}
void loop()
{
if (digitalRead(runSwitch) == 1)
{
unsigned long currentMillis = millis();
static unsigned long previousMillis;
if (currentMillis - previousMillis < interval)
{
previousMillis = currentMillis;
digitalWrite (mixerSwitch, HIGH);
Serial.print ("t = ");
Serial.println (currentMillis - previousMillis);
}
else
{
digitalWrite (mixerSwitch, LOW);
Serial.println("OFF");
}
}
else
{
digitalWrite (mixerSwitch, LOW);
checkKeypad();
checkScroll();
}
}
void checkScroll()
{
current_previous = debounce(last_previous, previous);
current_next = debounce(last_next, next);
//Page previous
if (last_previous == LOW && current_previous == HIGH)
{
if (page_counter < 3)
{ //Page counter never higher than 3(total of pages)
page_counter = page_counter + 1; //Page previous
}
else
{
page_counter = 1; //return to page 1
}
displayTopMenu();
}
last_previous = current_previous;
//Page next
if (last_next == LOW && current_next == HIGH)
{
if (page_counter > 1)
{ //Page counter never lower than 1 (total of pages)
page_counter = page_counter - 1; //Page next
}
else
{
page_counter = 3; //return to page 3
}
displayTopMenu();
}
last_next = current_next;
}
void displayTopMenu()
{
lcd1.clear();
switch (page_counter)
{
case 1:
lcd1.setCursor(0, 0);
lcd1.print("Menu: ");
lcd1.setCursor(0, 1);
lcd1.print("For Cement Press A");
lcd1.setCursor(0, 2);
lcd1.print("For Sand Press B");
lcd1.setCursor(0, 3);
lcd1.print("For Water Press C");
break;
case 2: //Design of page 3
lcd1.setCursor(5, 0);
lcd1.print("This is");
lcd1.setCursor(4, 1);
lcd1.print("Page 3");
break;
case 3: //Design of page 2
lcd1.setCursor(0, 0);
lcd1.print("For Settings Press D");
lcd1.setCursor(4, 1);
lcd1.print("Page 2");
break;
}
}
void displayLiveWeight()
{
lcd2.clear();
lcd2.print(" Weight Settings");
lcd2.setCursor(0, 1);
lcd2.print("Cement Sand Water");
lcd2.setCursor(0, 2);
lcd2.print(cementWeight);
lcd2.print(" kg");
lcd2.setCursor(8, 2);
lcd2.print(sandWeight);
lcd2.print(" kg");
lcd2.setCursor(14, 2);
lcd2.print(waterWeight);
lcd2.print(" kg");
}
void displaySetWeight()
{
lcd1.clear();
lcd1.print("Cement:");
lcd1.setCursor(7, 0);
lcd1.print(cementWeight);
lcd1.print(" kg");
lcd1.setCursor(0, 1);
lcd1.print("Sand :");
lcd1.setCursor(7, 1);
lcd1.print(sandWeight);
lcd1.print(" kg");
lcd1.setCursor(0, 2);
lcd1.print("Water :");
lcd1.setCursor(7, 2);
lcd1.print(waterWeight);
lcd1.print(" kg");
lcd1.setCursor(0, 3);
lcd1.print("Hold # to reset");
}
void checkKeypad()
{
// based on the keypad implementation for getKey() but now for all states
if (keypad.getKeys() && keypad.key[0].stateChanged) {
const char key = keypad.key[0].kchar;
const KeyState keyState = keypad.key[0].kstate;
Serial.print("kchar: ");
Serial.print(key);
Serial.print(", kstate: ");
Serial.println((keyState == PRESSED) ? "PRESSED"
: (keyState == HOLD) ? "HOLD"
: (keyState == RELEASED) ? "RELEASED"
: "IDLE");
switch (key)
{
case 'A':
if (keyState == PRESSED)
{
lcd1.clear();
lcd1.print("Enter Cement Weight");
lcd1.setCursor(0, 1);
lcd1.print("& Then Press #");
lcd1.setCursor(5, 2);
lcd1.print("kg");
cementWeight = getTheNumber();
displayLiveWeight();
}
break;
case 'B':
if (keyState == PRESSED)
{
lcd1.clear();
lcd1.print("Enter Sand Weight");
lcd1.setCursor(0, 1);
lcd1.print("& Then Press #");
lcd1.setCursor(5, 2);
lcd1.print("kg");
sandWeight = getTheNumber();
Serial.print (sandWeight);
displayLiveWeight();
}
break;
case 'C':
if (keyState == PRESSED)
{
lcd1.clear();
lcd1.print("Enter Water Weight");
lcd1.setCursor(0, 1);
lcd1.print("& Then Press #");
lcd1.setCursor(5, 2);
lcd1.print("kg");
waterWeight = getTheNumber();
displayLiveWeight();
}
break;
case 'D':
if (keyState == PRESSED)
{
displaySetWeight();
}
break;
case '#':
if (keyState == HOLD)
{
cementWeight = 0;
sandWeight = 0;
waterWeight = 0;
displaySetWeight();
displayLiveWeight();
}
break;
case '*':
if (keyState == PRESSED)
{
displayTopMenu();
}
break;
case NO_KEY: // fall through to default
default: ; // ignore
}
}
}
int getTheNumber()
{
char buffer[4];
// Input previous to 3 numbers until we find a * or #
int i = 0;
while (1)
{
if (keypad.getKeys() && keypad.key[0].stateChanged && keypad.key[0].kstate == PRESSED) {
const char key = keypad.key[0].kchar;
// If it's a number AND we have space left, add to our string
if ('0' <= key && key <= '9' && i < 3)
{
buffer[i] = key;
i++;
Serial.print(key);
lcd1.setCursor(i, 2);
lcd1.print(key);
}
// If it's a * or #, end
else if ('#' == key && i > 0)
{
// Null terminate
buffer[i] = 0;
int value = atoi(buffer); // Convert to an integer
i = 0;
return value;
break;
}
}
}
}