Arduino state not declared in this scope - arduino

I am currently working on a small project using arduino and the below is my code:
#include <SoftwareSerial.h>
#include <Adafruit_Fingerprint.h>
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {2, 3, 4, 5};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {6, 7, 8};
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin 13
#define LOCKED 2
#define PASSWORD_OK 1
#define UNLOCKED 0
//State Variables; initialise to locked state
int LockState = LOCKED;
int position = 0;
void setup()
{
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
//Initialize state and communications
setLockState(LOCKED);
Serial.begin(9600);
}
void loop()
{
char key = keypad.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
// case '*':
// digitalWrite(ledpin, LOW);
// break;
// case '#':
// digitalWrite(ledpin, HIGH);
// break;
default:
Serial.print(key);
}
}
}
It was from Adafruit. however, when i upload to my uno, it keeps giving me the error: 'setLockState' was not declared in this scope.
Can anyone shed some light on this?

I think you did not declare the function setLockState() in your code. I don't know about your project details, but it looks like you want to lock a thing via keypad or fingerprint. You can find a great Tutorial (including the missing setLockState() function) for a biometric-security-box on the website of Adafruit.

Related

Nokia 5110 display does not change on the key event in WeMos D1 with matrix keypad 3x4

I am working on a project with wemos, Nokia 5110 lcd and matric keypad 3x4.
I want to go to another screen on a specific key. The keyEvent() is the function I want to call when I trigger a key. The function is called correctly as it turns the led on when I press 3 on keypad, but the screen does not change.
Here is my code:
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Keypad.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(D7, D6, D5, D4, D3);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {D6, D5, D4, D3};
byte colPins[COLS] = {D2, D1, D0};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void keyEvent(int page, char key)
{
if(page == 1 && key == '3')
{
digitalWrite(D8, HIGH); //This line works fine. But the below code is not executed.
display.begin();
display.clearDisplay();
display.setCursor(0, 0);
display.println("Line 4");
display.println("Line 5");
display.println("Line 6");
display.display();
}
}
int list = 1;
void setup()
{
pinMode(D8, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char key = keypad.getKey();
if (key)
{
keyEvent(list, key);
}
display.begin();
display.setContrast(100);
display.clearDisplay();
display.setTextSize(0);
display.setTextColor(BLACK);
display.setCursor(0, 0);
display.println("Line 1");
display.println("Line 2");
display.println("Line 3");
display.display();
}

How to use Keypad_MC17 library in Arduino-Ide

I want to know the button pressed in the Serial-Monitor in Arduino-Ide. I am using Arduino-Uno Board and I have connected the MCP23017 IC via I2C Connection with Arduino-Uno board. I have connected the 5*5 Push-button Matrix with the pins of MCP23017. I am using the joe young/arduino_keypads library.I am not able to view the button pressed in the Serial-Monitor.
#include <Keypad_MC17.h>
#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#define I2CADDR 0
Adafruit_MCP23017 mcp_1;
const byte ROWS = 5; // five rows
const byte COLS = 5; // five columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','4','5'},
{'6','7','8','9','A'},
{'B','C','D','E','F'},
{'G','H','I','J','K'},
{'L' ,'M','N','O','P'}
};
byte rowPins[ROWS] = { 15 , 14 , 13 , 12 , 11 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 10 , 9 ,8 , 7 , 6 }; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad_MC17 customKeypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS, I2CADDR);
void setup(){
Serial.begin(9600);
Serial.println("Program Started" );
//Begin I2C Devices
Wire.begin();
Wire.beginTransmission(0);
if (Wire.endTransmission(0) == 0) Serial.println("I2C Device Not Found 0x00");
else Serial.println("I2C 0x00 Connected Successfully");
mcp_1.begin();
// Begin Keypad Device
customKeypad.begin( );
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey != NO_KEY) {
Serial.println(customKey);
}
}

Arduino Keypad Library on NodeMCU buttons mapped the wrong way

I am attaching a 4x4 membrane keypad to my NodeMCU (ESP8266-12E).
I am using the standard Keypad library for Arduino.
The layout of the keypad look like this:
1 2 3 A
4 5 6 B
7 8 9 C
* 0 # D
Everything works fine except the buttons A and B, which will behave like the buttons 1 and 4, so Pressing A gives me 1, and pressing B gives me 4.
I tried testing the same code on a Arduino Nano, the problem doesn't occur here.
I also tested the Keypad itself to see if it was wired correctly. The keypad is wired correctly.
What is causing the keypad to not work on the buttons A and B?
The code:
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte rowPins[ROWS] = {5,4,0,2};
byte colPins[COLS] = {14,12,13,15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13;
boolean blink = false;
boolean ledPin_state;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
digitalWrite(ledPin, HIGH); // Turn the LED on.
ledPin_state = digitalRead(ledPin); // Store initial LED state. HIGH when LED is on.
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}
void loop(){
char key = keypad.getKey();
if (key) {
Serial.println(key);
}
if (blink){
digitalWrite(ledPin,!digitalRead(ledPin)); // Change the ledPin from Hi2Lo or Lo2Hi.
delay(100);
}
}
// Taking care of some special events.
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
if (key == '#') {
digitalWrite(ledPin,!digitalRead(ledPin));
ledPin_state = digitalRead(ledPin); // Remember LED state, lit or unlit.
}
break;
case RELEASED:
if (key == '*') {
digitalWrite(ledPin,ledPin_state); // Restore LED state from before it started blinking.
blink = false;
}
break;
case HOLD:
if (key == '*') {
blink = true; // Blink the LED when holding the * key.
}
break;
}
}
The problem here appears to be the use of pin D8.
If you shift all of your connectors to D0 - D7 you will find that this works as expected

no matching function for call to LiquidCrystal::setCursor(int)

So, i was trying to make the cursor move on an lcd with a joystick but,
I type this:
int iniCursorX=6;
int iniCursorY=2;
#include <LiquidCrystal.h> //lcd
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //setting up lcd
// Arduino pin numbers
const int SW_pin = 6; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
int Y_pin = 1; // analog pin connected to Y output
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
lcd.print("Hello!");
analogWrite(Y_pin, INPUT);
}
void loop() {
lcd.setCursor(iniCursorX,iniCursorY);
lcd.cursor();
delay(200);
lcd.noCursor();
delay(200);
if(Y_pin=0, iniCursorX >= 2) {
int j=iniCursorX;
lcd.setCursor(j-1);
}
}
Then I Get this:
no matching function for call to LiquidCrystal::setCursor(int)
At the bottom of your code you have:
lcd.setCursor(j-1);
but lcd.setCursor takes two arguments.
Change it to something like lcd.setCursor(0, j-1); or anything you need.

send sms with sim900 using arduino

#include <Password.h>
#include <Keypad.h>
#include <Servo.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
Servo myservo;
Password password = Password( "1234" ); //password to unlock box, can be changed
SMSGSM sms;
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
const byte ROWS = 4;
const byte COLS = 4;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 };
int x=0;
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() //if i add sms(); function it workssss
{
Serial.begin(9600); //Start a Serial COM
Serial.println(F("ARDUINO SECURITY SYSTEM V1.0"));
Serial.print(F("Checking GSM COM..."));
if (gsm.begin(9600)) //Start the GSM COM
{
(sms.SendSMS("+XXXXX","Your Home Security system is powered up"));
Serial.println(F("Good To GO!!"));
}
else
{
Serial.println(F("Could not connect to GSM modem"));
}
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(11, OUTPUT); //green light
pinMode(12, OUTPUT); //red light
myservo.attach(13); //servo on digital pin 9 //servo
keypad.addEventListener(keypadEvent);//add an event listener for this keypad
}
void loop(){
keypad.getKey();
myservo.write(0);
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Enter : ");
Serial.println(eKey);
delay(10);
Serial.write(254);
switch (eKey){
case 'A': checkPassword(); delay(1); break;
case 'C': checkPassword(); delay(1); break;
case 'D': checkPassword(); delay(1); break;
case 'B': password.reset(); delay(1); break;
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey); delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){ //if password is right open box
Serial.println("Accepted");
Serial.write(254);delay(50);
//Add code to run if it works
myservo.write(5); //160deg
digitalWrite(11, HIGH);//turn on
delay(2000); //wait 5 seconds
digitalWrite(11, LOW);// turn off
}
else
{
Serial.println("Denied"); //if passwords wrong keep box locked
Serial.write(254);delay(10);
x++;
if(x==3)
//add code to run if it did not work
{
myservo.write(0);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
if (gsm.begin(9600))
{
(sms.SendSMS("+XXXXX","Your Home Security system is being bridged"));
Serial.println("USER WARNED");
}
}
}
}
;
}
In the picture the same code doesn't seem to work when I place the lines
if (gsm.begin(9600)) //Start the GSM COM
{
(sms.SendSMS("+8613668914901","Your Home Security system is being bridged"));
but this lines work great inside the void setup function.
How can I fix this problem? Inside the void setup the sketch works fine but when I also put the code in the function CheckPassword it doesn't send SMS.
I also tried to create a function let's say void SMS and call it in the checkPassword function but it doesn't solve the problem, btw the same function when called in the void setup works fine.
You are supposed to initialize the gsm only once, in the setup function.
In your code you attempt to initialize it again in the checkPassword method, and that is obviously not going to work.
Thus you should remove the line gsm.begin(9600) from the checkPassword function.
UPDATE 1:
In your scheme you reserve the pins 9, 8, 7, 6, 5, 4, 3, 2 for the Keyboard. However, at the same time you reserve pins 2, 3 for your GSM module (see GSM.cpp):
#define _GSM_TXPIN_ 2
#define _GSM_RXPIN_ 3
Using the same pins for multiple purposes can often result (if not done properly) in undefined behaviour which in the best scenario means that your sketch isn't doing what it is supposed to do, and in the worst scenario it might damage your components.
You are already using pins 0, 1 for the Serial library, but according to your code the pins 10, 11, 13 should still be free if you want to relocate the existing pins to your components.
Notice also the following warnings inside the GSM library:
[3] My shield doesn't work. Why?
Check this steps and then ask for support on the issues' page on google
code.
1) SIM900 and SIM908 require about 1 A during the hardest tasks.
You should have an external power source that can provide about
1 A at 8-12 V
2) If the SIM90X blinks (1 Hz) for some seconds and then turn off,
probably it's a communication's problem. Check the switch/jumpers
for Serial communication.
3) Arduino Uno has 2 KB of RAM. Library takes about 80% (we are working
to reduce it), if you use more than 20% left, Arduino can restart
or print on serial strange strings.
4) Check the jumper of communication, power source (battery or externel) and charge.

Resources