we are developing a low cost spectrophotometer , the device use 4x4 keypad , arduino uno, keypad, 2 servo motor (one to place sample and another one for grading), bluetooth module
abcd is use to choose sample
1,2,3,4 is use to change the grading degree
we have pre-installed data in the keypad ( button : 1,2,3,4,5) but we need the buttons to be able to overwrite and save it
can someone help us with this problem. thank you.
#include <Keypad.h>
#include <Servo.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void clearlcd(int x) //clear lcd function
{
lcd.setCursor(0,x);
for(int i=0; i<16; ++i)
{
lcd.write(' ');
}
}
int angle=0;
int redPin= A3;
int greenPin = A2;
int bluePin = A1;
const int ledPin = 5;
const int buzzerPin = 4;
const int ldrPin = A0;
const byte rows = 4; //four rows since the keypad is full
const byte columns = 4; //four columns, same as above
char buttons[rows][columns] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
int bluetoothTx = 0; //bluetooth tx to 0 pin
int bluetoothRx = 1; //bluetooth rx to 1 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); //for bluetooth
byte rowP[rows] = {13, 12, 11, 10}; //row pins of the keypad
byte columnsP[columns] = {9, 8, 7, 6}; //column pins of the keypad
Keypad pad = Keypad( makeKeymap(buttons), rowP, columnsP, rows, columns ); //create keypad
Servo ServoG;
Servo ServoS;//
//LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified module
void setup(){
ServoS.attach(2);
ServoG.attach(3); // attaches the servo on pin 9 to the servo object
//lcd
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0,0);
for (int positionCounter = 0; positionCounter < 35; positionCounter++) {
{
lcd.setCursor(16,0);
lcd.print(" Welcome to Group 5 ");
lcd.setCursor(17,1);
lcd.print("spectrophotometer");
}
lcd.scrollDisplayLeft(); // scroll one position left:
delay(200); // wait a bit:
}
lcd.clear();
//lcd.setCursor(4,0);
//lcd.print("For KEYPAD");
delay (1000);
for (int positionCounter1 = 0; positionCounter1 < 25; positionCounter1++) {
lcd.setCursor(0,0);
lcd.print(" Choose Sample : Press A - D ");
lcd.setCursor(0,1);
lcd.print(" Choose Grating : Press 1 - 6 ");
lcd.scrollDisplayLeft(); // scroll one position left:
delay(200); // wait a bit:
}
lcd.clear();
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ldrPin, INPUT);
bluetooth.begin(9600);
}
void loop(){
buzzerldrled();
char key = pad.getKey(); //get the char from the keypad
if(bluetooth.available()>0 ) //receive number from bluetooth
{
{
key = bluetooth.read(); //save the received number to toSend
}
}
if(key == 'A')
{
Serial.println("key = ");
Serial.print(key);
ServoS.write(0);
lcd.setCursor(1,0);
lcd.print("Sample 1");
}
if(key == 'B')
{
Serial.println("key = ");
Serial.print(key);
ServoS.write(45);
lcd.setCursor(1,0);
lcd.print("Sample 2");
}
if(key == 'C')
{
Serial.println("key = ");
Serial.print(key);
ServoS.write(90);
lcd.setCursor(1,0);
lcd.print("Sample 3");
}
if(key == 'D')
{
Serial.println("key = ");
Serial.print(key);
ServoS.write(45);
lcd.setCursor(1,0);
lcd.print("Sample 4");
}
if(key == '0')
{
Serial.println("key=");
Serial.print(key);
angle = 0;
ServoG.write(angle);
delay (1000);
//servograting.write(0);
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("reset");
setColor(0 ,0, 0); // Green Color
//delay(3000);
// lcd.clear();
}
if(key == '1')
{
Serial.println("key=");
Serial.print(key);
angle = 0;
ServoG.write(angle);
delay (1000);
lcd.setCursor(0,1);
lcd.print("Angle:25");
setColor(0, 255, 0); // Green Color
//delay(3000);
// lcd.clear();
}
if(key == '2')
{
Serial.println("key=");
Serial.print(key);
angle = 120;
ServoG.write(angle);
delay (1000);
lcd.setCursor(0,1);
lcd.print("Angle:120");
setColor(0, 0, 255); // Blue Color
//delay(3000);
//lcd.clear();
}
if(key == '3')
{
Serial.println("key=");
Serial.print(key);
angle = 50;
ServoG.write(angle);
delay (1000);
lcd.setCursor(0,1);
lcd.print("Angle:50");
setColor(170, 0, 255); // Purple Color
// delay(3000);
//lcd.clear();
}
if(key == '4')
{
Serial.println("key=");
Serial.print(key);
angle =90;
ServoG.write(angle);
delay (1000);
lcd.setCursor(0,1);
lcd.print("Angle:90");
setColor(255, 255, 255); // White Color
// delay(3000);
// lcd.clear();
}
if(key == '5')
{
Serial.println("key=");
Serial.print(key);
angle = 78;
ServoG.write(angle);
delay (1000);
lcd.setCursor(0,1);
lcd.print("Angle:78");
setColor(255, 255, 0); // Yellow Color
//delay(3000);
//lcd.clear();
}
if(key == '*'){
if (angle <180){
angle+=1;
ServoG.write(angle);
lcd.clear();
// lcd.print(angle);
lcd.setCursor(0,1);
lcd.print("Angle:");
lcd.print(angle);
}
}
if(key == '#'){
if (angle >0){
angle-=1;
ServoG.write(angle);
lcd.clear();
//lcd.print(angle);
lcd.setCursor(0,1);
lcd.print("Angle:");
lcd.print(angle);
}
}
// increment();
/* //Read from bluetooth and write to usb serial
if(bluetooth.available()>0 ) //receive number from bluetooth
{
{
int toSend = bluetooth.read(); //save the received number to toSend
Serial.print(toSend); //serial print toSend current number received from bluetooth
//ServoG.write(toSend); //to transfer input from mobile app to servo2
ServoS.write(toSend); //to transfer input from mobile app to servo2
}
{
int toSend = bluetooth.read(); //save the received number to toSend
Serial.print(toSend); //serial print toSend current number received from bluetooth
ServoG.write(toSend); //to transfer input from mobile app to servo2
//ServoS.write(toSend); //to transfer input from mobile app to servo2
}
}
*/
}
void buzzerldrled() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus >= 400) {
tone(buzzerPin, 100);
digitalWrite(ledPin, HIGH);
delay(100);
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
delay(100);
Serial.println("----------- ALARM ACTIVATED -----------");
lcd.setCursor(0,1);
lcd.print(" ALARM ACTIVATED !!!!!");
delay(500);
lcd.clear();
}
else {
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
//Serial.println("ALARM DEACTIVATED");
//lcd.setCursor(0,1);
//lcd.print("ALARM DEACTIVATED");
}
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}