Menu type display in LCD using 4x4 Keypad in Arduino - arduino-uno

I am trying to display two menu type in 16x2 LCD display using keypad in Arduino.
Here is the code;
`#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
byte rowPins[4]={9,8,7,6};
byte colPins [4]={5,4,3,2};
char keys[4][4]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
LiquidCrystal_I2C lcd(0x27,16,2);
boolean present = false;
boolean next = false;
boolean final = false;
String num1, num2; //
float ans;//
char op;//
//String Line1 = " ";
//String Line2 = " ";
//char cursors = 0;
//
////phaseshift
//char phase_value[2];
//int phase_cursor;
//int phase_index;
//char phasesign;
//
////declare function
//void Phaseshift_Fn();
//void CV_Fn();
//void CC_Fn();
//void Remote_Fn();
void setup() {
lcd.init();
lcd.backlight();
lcd.begin (16,2);
lcd.clear();
lcd.setCursor(0, 0); lcd.print(Line1);
lcd.setCursor(0, 1); lcd.print(Line2);
delay(100);
lcd.setCursor(5,0);
lcd.print("HELLO");
lcd.setCursor(0,1);
lcd.print("PRESS FWD Button");
delay(2000);
lcd.clear();
delay(1000);
}
void loop() {
char key = keypad.getKey();
if(key == 'B'){
lcd.setCursor(0,0);
lcd.print("IF NO AIR BUBBLE");
lcd.setCursor(0,1);
lcd.print("PRESS STOP");
}
if(key == 'A'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PRESS MODE");
lcd.setCursor(0,1);
switch(mode)
{
case Startup:
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
Line1 = "Enter Amount";
lcd.print(Line1);
lcd.setCursor(0,1);
Line2 = "Enter Time";
lcd.print(Line2);
delay(1000);
mode = Mainmenu;
break;
}
if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
{
if (present != true)
{
num1 = num1 + key; // storing the value of key pressed in num1
float numLength = num1.length();
lcd.setCursor(0, 1); /* decaling the place where the first entry will be displayed*/
lcd.print(num1); // printing the first number entered
}
else
{
num2 = num2 + key;//storing the value of second key pressed in num2
float numLength = num2.length();
lcd.setCursor(4, 1);/*decaling the place where the second entry will be displayed*/
lcd.print(num2); //printing the second number entered
final = true;
}
}
}`
As a beginner of the Arduino programming I tried so many methods including switch methods also.
But it didn't display. Please anyone can help me to solve this code?
I am trying to display this;enter image description here
Here I want to display
when I click the S button there two menus are displaying. They are Stop and Start. I want to display that two in LCD and if start button click then machine is forward after that when I click the stop button it should be stopped.

Related

NodeMCU Get data from webswerver and update value

I am using Nodenmcu with arduino IDE . I have used DHT11 IC to read temperature and humidity. Now i would like to add parameter in web page called set_temp . When Set_temp value being set to value the value should get updated when change icon has been pressed. Here is my code. My code is working till to enter the text from web but it wont update set_temp value
#include <ESP8266WiFi.h>
#include "DHT.h"
static float Set_Temp;
DHT dht;
int value = LOW;
const char* ssid = "esp8266";
const char* password = "Test123456";
int ledPin = 13; // GPIO13
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
dht.setup(D3); /* D1 is used for data communication */
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// New code has been added
delay(dht.getMinimumSamplingPeriod()); /* Delay of amount equal to sampling period */
float humidity = dht.getHumidity(); /* Get humidity value */
float temperature = dht.getTemperature(); /* Get temperature value */
// Serial.print(dht.getStatusString()); /* Print status of communication */
if(temperature>=Set_Temp)
{
digitalWrite(ledPin, HIGH);
value = HIGH;
}else
{
digitalWrite(ledPin, LOW);
value = LOW;
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
/* if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}*/
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Relay_Turn_On_Status: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
// client.println("<br><br>");
// client.println("<button>Turn On </button>");
// client.println("<button>Turn Off </button><br />");
// client.println("</html>");
client.print("</html>");
client.print("<head>");
client.print("<title>My Page</title>");
client.print("</head>");
client.print("<body>");
client.print("<br><br>");
client.print("Set_Temp: ");
client.println("<input type=text name=textbox size=5 value=Enter_Temp_Here");
client.println("<br><input type=submit value=Change ><br>");
client.println("</div>");
client.println("</body>");
client.println("</html>");
Set_Temp:"<br><input type=submit value=Change ><br>";
//Set_Temp:"<input type=text name=textbox size=5 value=Enter_Temp_Here>";
Serial.println(Set_Temp);
client.println("<br><br>");
client.println("DHT11_HumidityReading: ");
client.println(humidity,1);
client.println("<br><br>");
client.println("DHT11_Temprature Reading: ");
client.println(temperature,1);
client.println("<br><br>");
client.println("Set_Temp: ");
client.println(Set_Temp);
client.println("<br><br>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
My main doubt in this part of code where once i read it wont update.
Set_Temp:"<br><input type=submit value=Change ><br>";
//Set_Temp:"<input type=text name=textbox size=5 value=Enter_Temp_Here>";
Serial.println(Set_Temp);
If i use like this. it will display -1
Set_Temp=readString.indexOf(2);
I followed the method of get & post from HTTP https://www.w3schools.com/htmL/[Serial Out Here][web page]
How can i change my Set_Temp value from text box.
Take a look to this article, I follow it to turn on and turn off a light in NodeMCU with Arduino IDE. The client in the web is written on PHP.
https://blog.nyl.io/esp8266-led-arduino/
<?php
$light = $_GET['light'];
if($light == "on") {
$file = fopen("light.json", "w") or die("can't open file");
fwrite($file, '{"light": "on"}');
fclose($file);
}
else if ($light == "off") {
$file = fopen("light.json", "w") or die("can't open file");
fwrite($file, '{"light": "off"}');
fclose($file);
}
?>
I hope this help you.
Regards

How to redefine a variable character for the first time in arduino

i have this code to Comparison two char
char pass[]="1";
char passin[100]="";
and code to put characters in variable
void loop() {
char key= keypad.waitForKey(); // read char from keypad
strcat(passin,key);
if(strcmp(pass,passin)==0)
{ lcd.clear();
lcd.print("login success");
}else
{ lcd.clear();
lcd.print("login failed");
lcd.setCursor(0,1);
}
}
delay (1000);
}
what the way to return the value of passin as define like first time
If you just want to compare one character with another you can use the "==" operator. Note that to declare a character you use single quotes e.g. 'a'.
The code can be like this:
char pass = '1';
char key;
void loop(){
key = keypad.waitForKey(); // read char from keypad
if(pass == key){
lcd.clear();
lcd.print("login success");
}else {
lcd.clear();
lcd.print("login failed");
lcd.setCursor(0,1);
}
delay(1000);
}
The character key will be set to the new value every iteration of the loop.

Holding a push button for 3 second in a for loop

I have a button that basically has two functions. The aim is:
First push will blink a LED 15 times.
If someone holds the button longer than 3 seconds while the LED is blinking, it should stop and go back to the initial.
So I checked the Arduino's hold a button page and came up with this code. The problem is that I can not properly stop the blinking. The line Serial.println("DONE!!"); never works.
Where should I check if the button is held or not and should I use interrupt to end the for loop?
Here is my code:
int inPin = 2; // the pin number for input (for me a push button)
int ledPin = 9;
int current; // Current state of the button
// (LOW is pressed b/c I'm using the pullup resistors)
long millis_held; // How long the button was held (milliseconds)
long secs_held; // How long the button was held (seconds)
long prev_secs_held; // How long the button was held in the previous check
byte previous = LOW;
unsigned long firstTime; // how long since the button was first pressed
long millis_held2; // How long the button was held (milliseconds)
long secs_held2; // How long the button was held (seconds)
long prev_secs_held2; // How long the button was held in the previous check
byte previous2 = LOW;
unsigned long firstTime2; // how long since the button was first pressed
void setup() {
Serial.begin(9600); // Use serial for debugging
pinMode(ledPin, OUTPUT);
digitalWrite(inPin, INPUT); // Turn on 20k pullup resistors to simplify switch input
}
void loop() {
current = digitalRead(inPin);
// if the button state changes to pressed, remember the start time
if (current == HIGH && previous == LOW && (millis() - firstTime) > 200) {
firstTime = millis();
}
millis_held = (millis() - firstTime);
secs_held = millis_held / 1000;
// This if statement is a basic debouncing tool, the button must be pushed for at least
// 100 milliseconds in a row for it to be considered as a push.
if (millis_held > 50) {
// check if the button was released since we last checked
if (current == LOW && previous == HIGH) {
// HERE YOU WOULD ADD VARIOUS ACTIONS AND TIMES FOR YOUR OWN CODE
// ===============================================================================
//////////////////////////////////////////////////////// Button pressed Blink 15 times.
if (secs_held <= 0) {
for (int i = 0; i < 15; i++) {
ledblink(1, 750, ledPin);
current = digitalRead(inPin);
if (current == HIGH && previous == LOW && (millis() - firstTime2) > 200) {
firstTime2 = millis();
}
millis_held2 = (millis() - firstTime2);
secs_held2 = millis_held2 / 1000;
if (millis_held2 > 50) {
Serial.print("previousA ");
Serial.print(previous);
Serial.print(" currentA ");
Serial.println(current);
current = digitalRead(inPin);
if (current == HIGH && previous2 == HIGH) {
Serial.println("ALMOST! ");
Serial.println(secs_held2);
if (secs_held2 >= 2 && secs_held2 < 6) {
Serial.println("DONE!!");
}
}
}
previous2 = current;
prev_secs_held2 = secs_held2;
}
}
}
}
previous = current;
prev_secs_held = secs_held;
}
void ledblink(int times, int lengthms, int pinnum) {
for (int x = 0; x < times; x++) {
digitalWrite(pinnum, HIGH);
delay (lengthms);
digitalWrite(pinnum, LOW);
delay(lengthms);
}
}
This is how it would be done without using delay():
int inPin = 2;
int ledPin = 9;
long pressTimer;
long blinkTimer;
long blinkHalfDelay = 500; //milliseconds
int blinkCount = 0;
int blinkLimit = 15;
bool blinkFlag = false;
bool blinkLatch = true;
bool currentBtnState = false;
bool lastBtnState = false;
void setup() {
}
void loop() {
lastBtnState = currentBtnState;
currentBtnState = digitalRead(inPin);
//on rising edge, start timers
if(currentBtnState==true && lastBtnState==false)
{
pressTimer = millis();
}
//debounce activation
if(currentBtnState==true && !blinkFlag && millis()-pressTimer > 50)
{
blinkTimer = millis();
blinkCount = 0;
blinkLatch = true;
blinkFlag = true;
//functional execution would go here
}
if(currentBtnState = false || (millis()-pressTimer>3000 && currentBtnState == true))
{
blinkCount=blinkLimit;
blinkLatch = false;
}
if(blinkFlag == true)
{
if(millis()-blinkTimer > blinkHalfDelay)
{
blinkTimer = millis();
if(blinkLatch)
{
digitalWrite(pinnum, HIGH);
}
else
{
digitalWrite(pinnum, LOW);
blinkCount+=1;
}
}
if(blinkCount>blinkLimit)
{
blinkFlag = false;
digitalWrite(pinnum, LOW);
}
}
}
ps: sorry for not correcting your code directly, but it was kinda hard to read...

Arduino Serial.write to Processing returning 0?

I've currently got an Arduino program communicating accelerometer values via a Serial event to processing working perfectly. I'm trying to add a thermometer to my setup however processing is only receiving 0 from reading the pin. If I Serial.print the reading in setup it does print to the serial monitor just fine, however I can't get it to send proper values alongside my accelerometer readings.
Arduino code:
int inByte = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// send sensor values:
Serial.write(analogRead(A3)); // X AXIS
Serial.write(analogRead(A2)); // Y AXIS
Serial.write(analogRead(A1)); // Z AXIS
Serial.write(analogRead(A0)); // TEMPERATURE
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
Processing code:
import processing.serial.*;
Serial myPort;
int[] serialInArray = new int[4];
int serialCount = 0;
int xInput, yInput, zInput;
float temperature;
boolean firstContact = false;
void setup() {
size(600, 600, P3D);
pixelDensity(2);
noStroke();
background(0);
printArray(Serial.list());
String portName = Serial.list()[4];
myPort = new Serial(this, portName, 9600);
}
void draw() {
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
} else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
zInput = serialInArray[0]-80;
yInput = serialInArray[1]-80+69;
xInput = serialInArray[2]-77;
temperature = serialInArray[3]; // should return voltage reading (i.e 16Âșc = 130);
//println("x = " + xInput + ", y = " + yInput + ", z = " + zInput + ", Temp = " + serialInArray[3]);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
The accelerometer values print perfectly, but temperature just returns a 0. Serial.print(analogRead(A0)) in the serial monitor gives me the correct values, so the thermometer is definitely working.
Any help would be greatly appreciated, thank you!
In this line ,
if (serialCount > 2 ) {
change for
if (serialCount >= 4 ) {
OR try to use typecasting or change the temperature for integer !!!
int temperature;

arduino "error: expected unqualified-id before '=' token"

I have my sketch(simplified version) as shown:
int sensorPin = 0; // select the input pin for the photocell
int ledPin = 11; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int auto = 0;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("Digital reading = ");
Serial.println(sensorValue); // the raw analog reading
// turn the ledPin on
if (auto > 1)
if (sensorValue < 700){
digitalWrite(ledPin, LOW);
}else{
digitalWrite(ledPin,HIGH);
}
// stop the program for <sensorValue> milliseconds:
delay(10);
}
however it shows the error shown and highlights this line. int auto = 0; I have tried everything in my power, such as moving it, and changing it to a Boolean, but i can't get it to work.
"auto" is a keyword meaning, that a variable is automatically given the data type of its initialiser. Replace it with a non-reserved work and it will compile.
This IDE does not syntax it. Where np++ and others does.

Resources