#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.
Related
Can Anyone Please Convert the following Arduino Code to Embedded c code? I am very thankful to the one who converts this to an embedded c code. (this code is for Arduino lcd interfacing with Ultrasonic sensor)
#include <LiquidCrystal.h>
int inches = 0;
int cm = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
pinMode(7, INPUT);
}
void loop() {
lcd.clear();
cm = 0.01723 * readUltrasonicDistance(7);
inches = (cm / 2.54);
if (cm<40){
lcd.setCursor(0, 0);
// print the number of seconds since reset:
lcd.print("Caution: ");
lcd.setCursor(0,1);
lcd.print("Objects Nearby");
delay(1000);
}
}
long readUltrasonicDistance(int pin)
{
pinMode(pin, OUTPUT); // Clear the trigger
digitalWrite(pin, LOW);
delayMicroseconds(2);
// Sets the pin on HIGH state for 10 micro seconds
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
// Reads the pin, and returns the sound wave travel time in microseconds
return pulseIn(pin, HIGH);
}
I'm sorry but you can't because the dependencies of this code are LiquidCrystal.h (written in C++ and it contains dependencies like Arduino.h or Wire.h, libraries exclusive of Arduino software) and because methods like pinMode(int,int),digitalWrite(int,int),delayMicroseconds(int) comes from Arduino.h.
You can make your LiquidCrystal library rewriting it from the original.
Here some resources: C header and source, compile and upload, setup, standard avr libs.
I hope this can help. Good luck!
You can do that but is bit painful. Convert the class based function into c type functions. Remove dependant functions and replace the function by your own.
and use int main(void) instead of void loop().
I have an Arduino UNO R3 that reads a specific value from my Web Page.
I have an LED attached to the PIN 13 & GND of my Arduino.
When the Arduino reads 1 from my Web Page, it should turn the LED ON. When it reads 0, it should turn it off.
Following is the code for that:
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "inetGSM.h"
InetGSM inet;
#define ledPin 13
char msg[165];
char store[2];
char a;
char b;
char* disp;
boolean started=false;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
//Serial connection.
Serial.begin(9600);
Serial.println("GSM Shield testing.");
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(2400)) {
Serial.println("\nstatus=READY");
started=true;
} else Serial.println("\nstatus=IDLE");
if(started)
{
//GPRS attach, put in order APN, username and password.
//If no needed auth let them blank.
if (inet.attachGPRS("TATA.DOCOMO.INTERNET", "", ""))
Serial.println("status=ATTACHED");
else Serial.println("status=ERROR");
delay(1000);
//TCP Client GET, send a GET request to the server and
//save the reply.
//Print the results.
}
}
void loop()
{
inet.httpGET("www.boat.esy.es", 80, "/retrieve.php", msg, 165);
disp = strstr(msg,"\r\n\r\n");
disp = disp+4;
a = disp[0];
b = disp[1];
Serial.println(b);
if(b=='1')
{
digitalWrite(ledPin, HIGH);
}
if(b=='0');
{
digitalWrite(ledPin, LOW);
}
}
Problem here is, when I disable the digitalWrite(ledPin,LOW), that is when I comment it out, the LED turns on & stays that way.
But the moment I enable it & load the code on my Arduino, it won't even turn on.
I'm wondering if it's a logical error or something else. Because the turning on & off of the LED depends completely on the conditions being satisfied. And for now, my Web Page returns only 1, hence the LED should stay on. But when I include both digitalWrite(ledPin, HIGH) and digitalWrite(ledPin, LOW) in the same code and run it, it doesn't work. I can see the Serial printing out the messages associated with the LED ON, but I don't see the LED turning ON.
Thank You for your time!!
First of all you have a semicolon that I think should not be in your second if-statement?
if(b=='0'); <--
{
digitalWrite(ledPin, LOW);
}
Start by trying to remove that and see if there is a difference.
The code is below, slightly modified from a tutorial. I am working on a Teensy3.1. I added #include <SoftwareSerial.h> but it did not help. I also tried SerialUSB.begin(9600); instead of WiredSerial.begin(9600); //use native port on Due
#include <SoftwareSerial.h>
//minimal sketch for connection to ADS129n family. Load this script and open Tools/SerialMonitor.
//You should see text like this
// Device Type (ID Control Register): 62 Channels: 8
//If you see "Channels: 0" then check your wiring
#include "ads1298.h"
#include "adsCMD.h"
#include <Arduino.h>
#include <SPI.h> // include the SPI library:
int gMaxChan = 0; //maximum number of channels supported by ads129n = 4,6,8
int gIDval = 0; //Device ID : lower 5 bits of ID Control Register
int activeSerialPort = 0; //data will be sent to serial port that last sent commands. E.G. bluetooth or USB port
const int kPIN_LED = 13;//pin with in-built light - typically 13, 11 for Teensy 2.0.
#if defined(__SAM3X8E__)
#define isDUE //Detect Arduino Due
#define WiredSerial SerialUSB //Use Due's Native port
#else
#define WiredSerial Serial
#endif
void setup(){
using namespace ADS1298;
//prepare pins to be outputs or inputs
//pinMode(PIN_SCLK, OUTPUT); //optional - SPI library will do this for us
//pinMode(PIN_DIN, OUTPUT); //optional - SPI library will do this for us
//pinMode(PIN_DOUT, INPUT); //optional - SPI library will do this for us
pinMode(IPIN_CS, OUTPUT);
pinMode(PIN_START, OUTPUT);
pinMode(IPIN_DRDY, INPUT);
//pinMode(PIN_CLKSEL, OUTPUT);//*optional
//pinMode(IPIN_RESET, OUTPUT);//*optional
//pinMode(IPIN_PWDN, OUTPUT);//*optional
//start small peripheral interface
SPI.begin();
SPI.setBitOrder(MSBFIRST);
#ifndef isDUE
SPI.setClockDivider(SPI_CLOCK_DIV4); //http://forum.pjrc.com/threads/1156-Teensy-3-SPI-Basic-Clock-Questions
#endif
SPI.setDataMode(SPI_MODE1);
//Start ADS1298
delay(500); //wait for the ads129n to be ready - it can take a while to charge caps
adc_send_command(SDATAC); // Send SDATAC Command (Stop Read Data Continuously mode)
delay(10);
// Determine model number and number of channels available
gIDval = adc_rreg(ID); //lower 5 bits of register 0 reveal chip type
switch (gIDval & B00011111 ) { //least significant bits reports channels
case B10000: //16
gMaxChan = 4; //ads1294
break;
case B10001: //17
gMaxChan = 6; //ads1296
break;
case B10010: //18
gMaxChan = 8; //ads1298
break;
case B11110: //30
gMaxChan = 8; //ads1299
break;
default:
gMaxChan = 0;
}
//start serial port
SerialUSB.begin(9600); //use native port on Due
//WiredSerial.begin(9600); //use native port on Due
while (WiredSerial.read() >= 0) {} //http://forum.arduino.cc/index.php?topic=134847.0
//while (!WiredSerial) ; //required by Leonardo http://arduino.cc/en/Serial/IfSerial (ads129n requires 3.3v signals, Leonardo is 5v)
delay(200); // Catch Due reset problem
pinMode(kPIN_LED, OUTPUT);
}
void loop()
{
WiredSerial.print("Device Type (ID Control Register): "); SerialUSB.print(gIDval); SerialUSB.print(" Channels: "); SerialUSB.println(gMaxChan);
digitalWrite(kPIN_LED, HIGH); // turn the LED on (HIGH is the voltage level)
if (gMaxChan > 0)
delay(500); //long pause if OK
else
delay(50); //rapid blink if error
digitalWrite(kPIN_LED, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
Try using Serial instead of SerialUSB, I've tried it while using Arduino UNO.
I am trying to finish a small project with a moisture sensor connected to a Fio V3.
I have also attach a Xbee S1 module to Fio's socket.
I have upload the following code to Fio:
int igrasia = 7;
void setup()
{
Serial1.begin(9600);
pinMode(igrasia, INPUT_PULLUP);
}
void loop(){
int sensorVal = digitalRead(igrasia);
if (sensorVal == HIGH) {
Serial1.println("0"); // Send OK to xbee
}
else {
Serial1.println("1"); // Send NOT OK to xbee
}
delay(5000);
}
On my computer using the Xbee USB explorer I am receiving correct data on X-CTU every 5 seconds.
Zero (0) while the sensor is outside a glass of water and one (1) while the sensor is in the glass of water.
I want to read these bytes to an Arduino Uno with a LCD screen attached and an Xbee shield. For this reason I have uploaded to Uno the following code:
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x38,16,2); // set the LCD address to 0x20 for a 16 chars
void setup(){
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
// pinMode(8, INPUT_PULLUP);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
lcd.init(); // initialize the lcd
}
void loop(){
if(Serial.available())
{
char getData = Serial.read();
if (getData == '1')
{
Serial.print(getData);
digitalWrite(13, HIGH);
lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print("ATTENTION !!!!");
lcd.setCursor (0,1); // go to start of 1st line
lcd.print("WET environment");
}
else {
Serial.print(getData);
digitalWrite(13, LOW);
lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print("dry environment");
lcd.setCursor (0,1); // go to start of 1st line
lcd.print("all looks good!");
}
}
}
It doesn't work properly :- (
I have correct functionality for 0 and while the sensor is outside the water. LCD monitor shows "dry environment".
But as soon as I place the sensor in the water, LCD is not working as required.
Even if I leave the sensor in the water the LCD still displays "dry environment".
I tried the sensor connected directly to Uno with the LCD attached and it works!
I suppose something is wrong with the serial.read() and/or my If / loop statement on UNO.
Any suggestions or advice?
When you transmit the data, you're sending it as a String "1", "0".
On the receiver, you're testing for characters '1', '0'. Strings are terminated with a null character (/u0000), whereas characters are not. Therefore the condition is always failed. You could try transmitting and testing characters only.
I am trying to write a program that receives string data from i2c and displays it on an LCD. The first time data is received to the arduino, it renders it, however subsequent i2c payloads are ignored. My onReceive function has a status line display on the second line of the lcd which display the seconds() field from the timer chip. The seconds number does not seem to increment. However, the per-second dot flash as rendered in loop() does continue to blink, so the mcu is not frozen.
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Time.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
}
void loop()
{
lcd.setCursor(15,1);
if (second() % 2 == 0)
lcd.write(".");
else
lcd.write(" ");
delay(100);
}
void receiveEvent(int howMany)
{
//char buf[howMany];
int i=0;
char output[16];
lcd.clear();
while(Wire.available())
{
char c = Wire.read(); // receive byte as a character
lcd.setCursor(i,0);
lcd.write(c);
i++;
//buf[i++]=c;
//buf[i+1]=0;
}
lcd.setCursor(0,1);
sprintf(output,"s%dNB%dI%d",second(),howMany,i);
lcd.write(output);
}
Your Arduino may be trapped in here:
while(Wire.available())
{
//...
Use:
if(Wire.available() > 0) {
//stuff
}
instead.