My project is about a counter system. The counter is working based on user input by keypad matrix. I have write the coding but when i upload the coding, the counter does not incremented/decremented. But the led for receiver at the sensor working just fine. I tried to see the sensor data on serial monitor but there is no data at all. the sensor detect obstacle but serial monitor shows no data. can anyone help me check my coding and tell me what i do wrong?`
#include <Keypad.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
boolean flag = false;
int visitor = 1;
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte pin_rows[ROW_NUM] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {7, 6, 5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
byte value = 0;
//----------- define IR
#define sensorPin1 12
#define sensorPin2 13
int sensorState1 = 0;
int sensorState2 = 0;
int count = 0;
int getNumber() {
char key = keypad.getKey();
switch (key) {
case NO_KEY:
break;
case '*':
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.print("Set counter: ");
display.display();
delay(200);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
value = value * 10;
value = value + key - '0';
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.println(value);
display.display();
delay(200);
break;
case '#':
flag = true;
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.print("Counter is now set");
display.setCursor(20, 10);
display.print("THANK YOU");
display.display();
delay(1000);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.print("No visitor: ");
display.display();
Serial.println(value);
return (value);
break;
}
}
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
display.clearDisplay();
Serial.begin(9600);
pinMode (sensorPin1, INPUT);
pinMode (sensorPin2, INPUT);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Press * to set counter");
display.display();
delay(200);
}
void loop() {
getNumber();
if (flag == true) {
flag = false;
while (1) {
sensorState1 = digitalRead(sensorPin1);
sensorState2 = digitalRead(sensorPin2);
if (sensorState1 == LOW && count != value) {
Serial.println(sensorState1);
count++;
delay(500);
}
else if (count == value) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(20, 0);
display.println("FULL");
display.setTextSize(1);
display.setCursor(20, 20);
display.println("PLEASE WAIT..");
display.display();
}
if (sensorState2 == LOW && count != 0) {
count--;
delay(500);
if (count == 0) {
visitor = 0;
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.println("No visitors ");
display.display();
delay(1000);
}
}
else if (visitor == 0)
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.println("No visitors ");
display.display();
delay(1000);
break;
}
else if (count > 0 && count < value) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(4);
display.setCursor(20, 0);
display.println(count);
display.display();
}
}
}
}
I have corrected my coding and it works just fine. but when i pressed '*' it does not goes back to the getNumber () function. can you tell me why and how to solve it?
#include <Keypad.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
boolean flag = false;
int visitor = 1;
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte pin_rows[ROW_NUM] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {7, 6, 5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
byte value = 0;
//----------- define IR
#define sensorPin1 12
#define sensorPin2 13
int sensorState1 = 0;
int sensorState2 = 0;
int count = 0;
int getNumber() {
char key = keypad.getKey();
switch (key) {
case NO_KEY:
break;
case '*':
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.print("Set counter: ");
display.display();
delay(200);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
value = value * 10;
value = value + key - '0';
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.println(value);
display.display();
delay(200);
break;
case '#':
flag = true;
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.print("Counter is now set");
display.setCursor(20, 10);
display.print("THANK YOU");
display.display();
delay(1000);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.print("No visitor: ");
display.display();
Serial.println(value);
return (value);
break;
}
}
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
display.clearDisplay();
Serial.begin(9600);
pinMode (sensorPin1, INPUT);
pinMode (sensorPin2, INPUT);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Press * to set counter");
display.display();
delay(200);
}
void loop() {
getNumber();
if (flag == true) {
flag = false;
while (1) {
sensorState1 = digitalRead(sensorPin1);
sensorState2 = digitalRead(sensorPin2);
if (sensorState1 == LOW && count != value) {
Serial.println(sensorState1);
count++;
delay(500);
}
else if (count == value) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(20, 0);
display.println("FULL");
display.setTextSize(1);
display.setCursor(20, 20);
display.println("PLEASE WAIT..");
display.display();
}
if (sensorState2 == LOW && count != 0) {
count--;
delay(500);
if (count == 0) {
visitor = 0;
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.println("No visitors ");
display.display();
delay(1000);
}
}
else if (visitor == 0)
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 0);
display.println("No visitors ");
display.display();
delay(1000);
break;
}
else if (count > 0 && count < value) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(4);
display.setCursor(20, 0);
display.println(count);
display.display();
}
}
}
}
I have corrected my coding and it works just fine. but when i pressed '*' it does not goes back to the getNumber () function. can you tell me why and how to solve it?