Hello everyone, my project is to control led diode and dc motor, test shield buttons, and check system run time. Testing buttons and run time i have done.
Deal is with led and dc motor control.
Issue is there that when i run code-diode and motor starts to work.
But they should start working only when i'm in in motor control menu or led control menu.
DC and diode part
void MotorControl() {
while (readKeypad() != btnSELECT) {
if (readKeypad() == btnUP) {
speed1 <= maxspeed;
speed1 += Upstep;
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
lcd.print(speed1);
delay(100);
analogWrite(mtor, speed1);
}
if (readKeypad() == btnDOWN) {
speed1 <= maxspeed;
speed1 -= Upstep;
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
lcd.print(speed1);
delay(100);
analogWrite(mtor, speed1);
}
}
}
//____________________________
void LEDControl() {
while (readKeypad() != btnSELECT) {
if (readKeypad() == btnUP) {
speed1 < 255;
speed1 += Upstep;
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
lcd.print(speed1);
delay(100);
analogWrite(led, speed1);
}
if (readKeypad() == btnDOWN) {
speed1 > 255;
speed1 -= Upstep;
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
lcd.print(speed1);
delay(100);
analogWrite(led, speed1);
}
}
}
//____________________________
All code.
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int masterKey = A0;
int keypadKey = 0;
int buttonPress = 0;
char push;
byte menu = 1;
byte menuOld = 1;
byte menuAll = 4;
//_______LED/MOTOR_________________
int speedall;
int mtor = 5,
led = 6,
error = 7;
const int Upstep = 3;
const int maxspeed = 255;
void setup() {
pinMode(led, OUTPUT);
pinMode(mtor, OUTPUT);
lcd.begin(16, 2);
mainMenu();
delay(100);
Serial.begin(9600);
}
void loop()
{
push = readKeypad();
menuContr();
if (push == btnSELECT) //enter selected menu
{
waitKey();
switch (menu) {
case 1:
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Push the buttons");
ButtonTest();
break;
case 2:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Speed 0 to 255:");
MotorControl();
break;
case 3:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Brightness 0 to 255:");
LEDControl();
break;
case 4:
SystemTime();
break;
}
mainMenu();
waitKey();
}
delay(10);
}
//____________________________
void ButtonTest() {
while (readKeypad() != btnSELECT) {
lcd.setCursor(9, 1);
lcd.print(millis() / 1000);
lcd.setCursor(0, 1);
int lcd_key = readKeypad();
switch (lcd_key) {
case btnRIGHT:
lcd.print("RIGHT ");
break;
case btnLEFT:
lcd.print("LEFT ");
break;
case btnUP:
lcd.print("UP ");
break;
case btnDOWN:
lcd.print("DOWN ");
break;
case btnSELECT:
lcd.print("SELECT");
break;
case btnNONE:
lcd.print("NONE ");
break;
}
}
}
//____________________________
void MotorControl() {
while (readKeypad() != btnSELECT) {
if (readKeypad() == btnUP) {
speedall <= maxspeed;
speedall += Upstep;
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
lcd.print(speedall);
delay(100);
analogWrite(mtor, speedall);
}
else if (readKeypad() == btnDOWN) {
speedall <= maxspeed;
speedall -= Upstep;
Serial.print(speedall);
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
delay(100);
analogWrite(mtor, speedall);
}
}
}
//____________________________
void LEDControl() {
while (readKeypad() != btnSELECT) {
if (readKeypad() == btnUP) {
speedall < 255;
speedall += Upstep;
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
lcd.print(speedall);
delay(100);
analogWrite(led, speedall);
}
else if (readKeypad() == btnDOWN) {
speedall > 255;
speedall -= Upstep;
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
lcd.print(speedall);
delay(100);
analogWrite(led, speedall);
}
}
}
//____________________________
void SystemTime() {
lcd.clear();
while (readKeypad() != btnSELECT) {
lcd.setCursor(6, 0);
lcd.print("TIME");
lcd.setCursor(7, 1);
lcd.print(millis());
lcd.print("s");
delay(100);
lcd.clear();
}
}
//______________________________
void mainMenu() {
lcd.clear();
lcd.setCursor(0, 0);
switch (menu) {
case 1:
Serial.print(speedall);
lcd.print("Button test");
break;
case 2:
Serial.print(speedall);
lcd.print("Motor contr");
break;
case 3:
Serial.print(speedall);
lcd.print("LED contr");
break;
case 4:
Serial.print(speedall);
lcd.print("Syst run time");
break;
}
}
//____________________________
void executeAction() {
switch (menu) {
case 1:
ButtonTest();
break;
case 2:
MotorControl();
break;
case 3:
LEDControl();
break;
case 4:
SystemTime();
break;
}
}
void menuContr() {
waitKey();
if (push == btnUP) {
menu++;
if (menu > menuAll)
menu = 1;
}
else if (push == btnDOWN) {
menu--;
if (menu == 0)
menu = menuAll;
}
if (menu != menuOld) {
mainMenu();
menuOld = menu;
}
}
char readKeypad() {
keypadKey = analogRead(masterKey);
if (keypadKey > 1000) return btnNONE; // about 1023
if (keypadKey < 50) return btnRIGHT; // about 0
if (keypadKey < 195) return btnUP; // about 99
if (keypadKey < 380) return btnDOWN; // about 255
if (keypadKey < 555) return btnLEFT; // about 409
if (keypadKey < 790) return btnSELECT; // about 640
return btnNONE;
}
void waitKey() {
while ( analogRead(masterKey) < 700) {}
}
[/code]