//INCUDES
#include <LiquidCrystal.h>
#include <IRremote.hpp>
#include <SharpIR.h>
#include <Arduino.h>
#include <Keypad.h>
#include <Servo.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
//DEFINES
#define flipSwitchPin 6 // Pin for the flip switch
#define lightPin 2 // Pin for the light
#define ledPin 3 // Pin for the sensor
#define sensorPin 4 // Pin for the Sharp IR sensor
//servo pin 5
uint8_t colPins[COLS] = { 9, 8, 7 }; // Pins connected to C1, C2, C3
uint8_t rowPins[ROWS] = { 13, 12, 11, 10 }; // Pins connected to R1, R2, R3, R4
//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
//VARIABLES
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
Servo myservo;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int isOn = 0;
int switchState;
int enteredCount = 0;
int itemCount = 0;
bool enterPressed = false;
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(flipSwitchPin, INPUT_PULLUP); // Set flip switch pin as input with internal pull-up resistor
pinMode(lightPin, OUTPUT); // Set light pin as output
pinMode(sensorPin, INPUT); // Set sensor pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
myservo.attach(5);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop() {
lcd.print("HELLO");
/* if (powerOn()) {
isON();
remoteInput();
} else {
powerOff();
}
delay(1000);*/
}
void blink() {
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(500); // Wait for 0.5 seconds
digitalWrite(ledPin, LOW); // Turn off the LED
delay(500); // Wait for 0.5
}
}
void sensorRun() {
//distance = mySensor.getDistance();
lcd.setCursor(0, 1);
lcd.print("Count: ");
lcd.print(itemCount);
for(int i = 0; i < enteredCount; i++){
myservo.write(1);
wait(1);
myservo.write(200);
if (digitalRead(sensorPin)) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
// we have just turned on
Serial.println("Motion detected!");
itemCount++;
delay(1000);
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
Serial.println("Motion ended!");
}
wait(2);
}
if (itemCount == enteredCount) {
// itemCount = 0;
// if (itemCount == 0) {
lcd.clear();
lcd.print("Count Done");
// ring(1000);
wait(4);
Reset(2);
} else {
lcd.clear();
lcd.print("Count Incomplete");
// ring(4000);
blink();
wait(2);
Reset(2);
}
}
/*void ring(int v) {
tone(buzzer, 3000); // Send 1KHz = 1000 sound signal...
delay(500);
tone(buzzer, 3000); // Send 1KHz = 1000 sound signal...
delay(500);
noTone(buzzer);
}*/
void wait(int t) {
t = t * 1000;
delay(t);
}
void remoteInput() {
lcd.clear();
char key = keypad.getKey();
if (key != NO_KEY) {
int value = getNumber(key);
if (key <= 9) {
enteredCount = (enteredCount * 10) + value;
} else if (value == 13 ) {
enteredCount /= 10;
} else if (value == 12 ) {
enterPressed = true;
// enteredCount = 0;
}
}
lcd.print(enteredCount);
if (enterPressed) {
sensorRun();
}
}
int getNumber(char key) {
switch (key) {
case '0': return 0; break;
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
case '*': return 12; break; //enter
case '#': return 13; break; //delete
default: return 44; break;
}
}
bool powerOn() {
switchState = digitalRead(flipSwitchPin); // Read the state of the flip switch
if (switchState == LOW) // If the flip switch is on
return true;
return false;
}
void powerOff() {
lcd.clear();
if (isOn > 0) Reset(0);
digitalWrite(lightPin, LOW); // Turn off the light
lcd.print("Device off");
noTone(buzzer);
delay(1000);
}
void isON() {
isOn++;
digitalWrite(lightPin, HIGH); // Turn on the light
if (isOn == 1 ) {
lcd.clear();
lcd.print("Device ON"); // print when switch is turned on once
delay(3000);
lcd.clear();
}
}
void Reset(int n) {
isOn = n;
enteredCount = 0;
itemCount = 0;
enterPressed = false;
lcd.clear();
lcd.print("Reset...");
wait(2);
lcd.clear();
}