Hjälp med program till arduino (C)
Jag behöver lite hjälp, jag håller på och bygger ett alarm med hjälp av en arduino uno, GSM-modul, RFID-läsare samt ett piezo-element. Tanken är att Arduinon ska startas när dörren öppnas (den strömsätts då), den ska börja köra igång gsm-skölden och sen läsa av ifall det finns ett RFID-kort framför läsaren, och om det finns ett sådant så ska den spela upp ett ljud och sen stänga av gsm-modulen och inte göra någonting mer. Om det inte presenteras ett RFID-kort ska den ringa upp ett nummer.
Jag har lyckats få min arduino att spela upp ljud, att läsa av RFID-kort samt att ringa upp mitt nummer med individuella program, men inte lyckats få det att samspela som jag vill. Såhär ser programmet ut:
const int gsmPin = 7; // the number of the gsm pin
int gsmState = HIGH; // gsmState used to set the GSM
int gsmOff = LOW;
#include <SoftwareSerial.h> //Library for the GSM-module
#include <SPI.h> //Library for RFID-reader
#include <MFRC522.h> //Library for the RFID-reader
#include <pitches.h> //Library for the piezo
#define SS_PIN 10 //SS-pin for the RFID-reader (SDA)
#define RST_PIN 9 //RST pin for the RFID-reader
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
SoftwareSerial Sim900Serial(2, 3);
void setup()
{
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
pinMode(gsmPin, OUTPUT);
digitalWrite(gsmPin, gsmState);
delay(20000);
Sim900Serial.begin(115200); // the GPRS baud rate
delay(2000);
Sim900Serial.println("AT+IPR=19200"); // Set the baud rate
delay(500);
Sim900Serial.begin(19200); // the GPRS baud rate
delay(1000);
}
int ljud = NOTE_C4;
void loop()
{
if ( mfrc522.PICC_IsNewCardPresent()){
delay(2000);
tone(8, ljud);
delay(2000);
noTone(8);
digitalWrite(gsmPin, gsmOff);
while(1);
}
else
Sim900Serial.println("ATD+46xxxxxxxx;");//'*'instead the phone number you want to dial
delay(100);
Sim900Serial.println();
delay(20000);
Sim900Serial.println("ATH");
while(1);
}
Det som händer för tillfället är att den ringer upp mitt nummer och sen lägger på, även fast jag har kortet framför läsaren. Någon som vet vad som kan vara fel?
Dessa övriga 3 program fungerar (gör det de ska)
const int gsmPin = 7; // the number of the gsm pin
// Variables will change:
int gsmState = HIGH; // gsmState used to set the GSM
// set the digital pin as output:
/*Note:This code is used for Arduino 1.0 or later*/
#include <SoftwareSerial.h>
SoftwareSerial Sim900Serial(2, 3);
void setup()
{
pinMode(gsmPin, OUTPUT);
digitalWrite(gsmPin, gsmState);
delay(30000);
Sim900Serial.begin(115200); // the GPRS baud rate
delay(2000);
Sim900Serial.println("AT+IPR=19200"); // Set the baud rate
delay(500);
Sim900Serial.begin(19200); // the GPRS baud rate
delay(1000);
}
void loop()
{
Sim900Serial.println("ATD+46xxxxxxx;");//'*'instead the phone number you want to dial
delay(100);
Sim900Serial.println();
delay(20000);
Sim900Serial.println("ATH");
while(1);
}
/*
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
* The library file MFRC522.h has a wealth of useful info. Please read it.
* The functions are documented in MFRC522.cpp.
*
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Released into the public domain.
*
* Sample program showing how to read data from a PICC using a MFRC522 reader on the Arduino SPI interface.
*----------------------------------------------------------------------------- empty_skull
* Aggiunti pin per arduino Mega
* add pin configuration for arduino mega
* http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Pin layout should be as follows:
* Signal Pin Pin Pin
* Arduino Uno Arduino Mega MFRC522 board
* ------------------------------------------------------------
* Reset 9 5 RST
* SPI SS 10 53 SDA
* SPI MOSI 11 52 MOSI
* SPI MISO 12 51 MISO
* SPI SCK 13 50 SCK
*
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Scan PICC to see UID and type...");
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Dump debug info about the card. PICC_HaltA() is automatically called.
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
/*
Melody
Plays a melody
circuit:
* 8-ohm speaker on digital pin 8
created 21 Jan 2010
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/Tone
*/
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.
}