Hey everyone,
So the problem is my code doesn't seem to be executing properly? I'm a bit confused. I initally had an IR breakbeam ckt on an UNO and it was tx'ing to a MEGA. All was well so I decided to try and implement the IR ckt onto the MEGA itself. It wont work at all now?
What have I done wrong? is it my code placement? Should I have it in an Interrput? I'm at a loss.
//Libraries Included in sketch
#include <VirtualWire.h>
#include<Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//LCD Display Declarations
const int rows = 4;
const int cols= 20;
//test LED
int ledR = 13;
int sensor_IR = 10;
int sensorValue;
//Keypad Declarations
const byte keyRows = 4;
const byte keyCols= 3;
//Define i2c address
#define I2C_ADD 0x27
#define B_light 3
//Display Variables
#define d4 4
#define d5 5
#define d6 6
#define d7 7
#define EN 2
#define RW 1
#define RS 0
//Initlize LCD
LiquidCrystal_I2C lcd(I2C_ADD, EN, RW, RS, d4, d5, d6, d7);
//Array for keypad
char keys[keyRows][keyCols] =
{{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},};
//Battery variable Declarations
int voltDiv = A0;
int divider;
int vRef=10;
int valNew = 0;
int percentVolt=0;
float voltage;
//LCD battery display
byte battery[8] =
{
0b00100,
0b01110,
0b01110,
0b01110,
0b01110,
0b01110,
0b01110,
0b01110
};
//Arduino pins keypad is connected to
byte rowPins[keyRows] = {5,6,7,8};
byte colPins[keyCols] = {2,3,4};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,keyRows,keyCols);
int count = 0;
char keyNew[3];
int amount;
void setup()
{
pinMode(sensor_IR, INPUT_PULLUP);
pinMode(ledR, OUTPUT);
//LCD settings
lcd.setBacklightPin(B_light,POSITIVE);
lcd.setBacklight(HIGH);
lcd.begin(20,4);
lcd.setCursor(5,1);
lcd.print("FOOTFALL COUNTER");
delay(2000);
lcd.clear();
/*
//RX setup
vw_set_ptt_inverted(true);
vw_set_rx_pin(12);
vw_setup(4000);
vw_rx_start();
*/
}
void loop()
{
sensorValue = digitalRead(sensor_IR);
if(sensorValue == LOW)
{
digitalWrite(ledR, HIGH);
}
else
{
digitalWrite(ledR,LOW);
}
//Battery Function call
batteryDisplay();
//Setup Display code
lcd.setCursor(0,1);
lcd.print("Plese Enter");
lcd.setCursor(0,2);
lcd.print("Expected customers:");
amount = GetCusto();
lcd.print(amount);
}
int GetCusto()
{
char key=keypad.getKey();
int n;
int count = 0;
for(n=0;n<3;n++)
{
keyNew[n] = keypad.waitForKey();
lcd.print(keyNew[n]);
count++;
}
while(count==3)
{
char key= keypad.getKey();
if (key == '#')
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("********************");
lcd.setCursor(0,1);
lcd.print("****** Stored ******");
lcd.setCursor(0,2);
lcd.print("********************");
lcd.setCursor(0,3);
lcd.print("********************");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Customers left:");
lcd.setCursor(10,1);
lcd.print(keyNew);
int r;
r = atoi(keyNew);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Customers left:");
lcd.setCursor(10,1);
lcd.print(r);
}
}
}
void batteryDisplay(void)
{
lcd.createChar(0, battery);
lcd.setCursor(15, 0);
lcd.write((uint8_t)0);
divider = analogRead(voltDiv);
voltage = (divider*vRef)/1023;
percentVolt = voltage/5*100;
lcd.setCursor(16,0);
lcd.print(percentVolt);
lcd.print("%");
delay(1000);
}
I have tried putting the code into the keypad function, I've tried a function call, Ive tried moving to different locations of the loop. Also should note that the reason for the keypad is a footfall counter. When a person walks by it will be deducted from a set value inputted at the start of the day.