Arduino - cannot get buzzer to buzz - arduino

I've hooked up a buzzer to pin 13 & ground on my Arduino.
The "Blink" example works fine and the buzzer sounds every second off and on as expected.
However, when I try to do the same buzz with my code, I can't get it to buzz upon a specific event. This is a homegrown security system - when the door is opened, I want the Arduino to check a PHP page which returns "armed" if the system has been armed.
Everything else seems to work except the buzzer part.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xD3, 0x6C };
char serverName[] = "mysite.com";
String currentLine = "";
String armed = "No";
int nFrontWindow = 0;
int sFrontWindow = 1;
int kitchenWindow = 2;
int bedroomWindow = 3;
int frontDoor = 4;
int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int threshold0 = 20;
int threshold1 = 20;
int threshold2 = 20;
int threshold3 = 20;
int threshold4 = 20;
int breach0 = 0;
int breach1 = 0;
int breach2 = 0;
int breach3 = 0;
int breach4 = 0;
int alarm0 = 0;
int alarm1 = 0;
int alarm2 = 0;
int alarm3 = 0;
int alarm4 = 0;
EthernetClient client;
void setup() {
pinMode(13, OUTPUT);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
if(Ethernet.begin(mac) == 0) { // start ethernet using mac & IP address
Serial.println("Failed to configure Ethernet using DHCP");
while(true) // no point in carrying on, so stay in endless loop:
;
}
delay(1000); // give the Ethernet shield a second to initialize
}
void loop() {
// read the analog in values:
val0 = analogRead(nFrontWindow);
val1 = analogRead(sFrontWindow);
val2 = analogRead(kitchenWindow);
val3 = analogRead(bedroomWindow);
val4 = analogRead(frontDoor);
// print the analog in values:
if (val0 > threshold0)
{
Serial.print("nFrontWindow: ");
Serial.print(val0);
Serial.print("\n");
if (alarm0)
{
if (breach0 < 10)
breach0++;
}
else
{
if (breach0 > 9)
{
alarm0 = 1;
send_alert(0);
}
else
breach0++;
}
}
else
{
if (alarm0)
{
if (breach0 > 0)
breach0--;
else
{
alarm0 = 0;
send_alert(10);
}
}
else
{
if (breach0 > 0)
breach0--;
}
}
if (val1 > threshold1)
{
Serial.print("sFrontWindow: ");
Serial.print(val1);
Serial.print("\n");
if (alarm1)
{
if (breach1 < 10)
breach1++;
}
else
{
if (breach1 > 9)
{
alarm1 = 1;
send_alert(1);
}
else
breach1++;
}
}
else
{
if (alarm1)
{
if (breach1 > 0)
breach1--;
else
{
alarm1 = 0;
send_alert(11);
}
}
else
{
if (breach1 > 0)
breach1--;
}
}
if (val2 > threshold2)
{
Serial.print("kitchenWindow: ");
Serial.print(val2);
Serial.print("\n");
if (alarm2)
{
if (breach2 < 10)
breach2++;
}
else
{
if (breach2 > 9)
{
alarm2 = 1;
send_alert(2);
}
else
breach2++;
}
}
else
{
if (alarm2)
{
if (breach2 > 0)
breach2--;
else
{
alarm2 = 0;
send_alert(12);
}
}
else
{
if (breach2 > 0)
breach2--;
}
}
if (val3 > threshold3)
{
Serial.print("bedroomWindow: ");
Serial.print(val3);
Serial.print("\n");
if (alarm3)
{
if (breach3 < 10)
breach3++;
}
else
{
if (breach3 > 9)
{
alarm3 = 1;
send_alert(3);
}
else
breach3++;
}
}
else
{
if (alarm3)
{
if (breach3 > 0)
breach3--;
else
{
alarm3 = 0;
send_alert(13);
}
}
else
{
if (breach3 > 0)
breach3--;
}
}
if (val4 > threshold4)
{
Serial.print("frontDoor: ");
Serial.print(val4);
Serial.print("\n");
if (alarm4)
{
if (breach4 < 10)
breach4++;
}
else
{
if (breach4 > 9)
{
alarm4 = 1;
send_alert(4);
}
else
breach4++;
}
}
else
{
if (alarm4)
{
if (breach4 > 0)
breach4--;
else
{
alarm4 = 0;
send_alert(14);
}
}
else
{
if (breach4 > 0)
breach4--;
}
}
delay(100);
}
void send_alert(int pin)
{
if (client.connect(serverName, 80)>0) {
client.flush();
Serial.print("\nconnected... ");
String link = "GET [PHP FILE GOES HERE]";
link += pin;
link += " HTTP/1.1";
Serial.print("Sending alert code: ");
Serial.print(pin);
Serial.print("\n\n");
client.println(link);
client.println("Host: mysite.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
delay(3000);
} else {
Serial.println("connection failed");
//handle this
}
Serial.print("Server response:\n");
Serial.print("----------------\n");
while(client.available()){
char c = client.read();
Serial.print(c);
currentLine += c;
if (c == '\n') {
currentLine = "";
}
if (currentLine.endsWith("armed"))
{
if (pin == 4 || pin == 3 || pin == 2 || pin == 1 || pin == 0)
{
Serial.print("\nBZZZZZZZZZZ\n");
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
}
Serial.print("\n----------------\n");
//else
// Serial.println("result not found");
client.stop();
client.flush();
}

I think your problem is that pin 13 is used by the Ethernet Shield. So are pin 10, 11, and 12. So use a pin other then them.

I think you problem is that you aren't generating a square wave to make sound the buzzer.
Since you are using Arduino you can just use tone() and noTone() functions, well explained in Arduino site.

Related

How can I get out of the loop?

I'd like to get out of the loop in my Arduino project. Currently I am programming a digital watch, everything works just fine but I wanted to add options menu by clicking a button, but after clicking it nothing pops up even if I have something inside the code. Take a look at it. I don't know how to write it better. If you have some ideas, please you could rewrite some parts of the code and explain why you did so. Thanks forwardly.
Please do not mind s = s + 1, I wanted it like that.
#include "LiquidCrystal.h"
#include <EEPROM.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int h = 0;
int m = 0;
int s = 0;
int right = 8;
int left = 9;
int buttonStateLeft = 0;
String when;
uint8_t EEPROMaddress_sec = 1;
uint8_t EEPROMaddress_min = 2;
uint8_t EEPROMaddress_hour = 3;
bool clockShown = true;
bool menuShown = false;
void setup()
{
lcd.begin(16,2);
pinMode(right, INPUT);
pinMode(left, INPUT);
}
void loop()
{
if(menuShown)
{
lcd.setCursor(0,0);
lcd.print("jozo je kkt");
delay(200);
}
if(clockShown) {
lcd.setCursor(0,0);
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}
s = EEPROM.read(EEPROMaddress_sec);
m = EEPROM.read(EEPROMaddress_min);
h = EEPROM.read(EEPROMaddress_hour);
s = s + 1;
if(h > 12)
when = "PM";
if(h < 12)
when = "AM";
if(h == 12)
when = "PM";
lcd.print("Cas: ");
if(h<10)lcd.print("0");
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);
lcd.print(" ");
lcd.print(when);
if(s == 60)
{
s = 0;
m = m+1;
}
if(m == 60)
{
s = 0;
m = 0;
h = h+1;
}
if(h == 24)
{
m = 0;
s = 0;
h = 0;
}
EEPROM.write(EEPROMaddress_sec, s);
EEPROM.write(EEPROMaddress_min, m);
EEPROM.write(EEPROMaddress_hour, h);
delay(1000);
}
}
In order to do that you will have to use Interrupts, note that you must connect your button to an interrupt pin (not every pin is an interrupt pin) you can google "what are the interrupt pins of 'your_card_name' ", the code would have to change, you can follow these :
In the setup function replace :
pinMode(left, INPUT);
by :
attachInterrupt(digitalPinToInterrupt(left), switchMode, RISING);
add this function before setup(){...}
int lastPressTime=millis();
void switchMode(){ // function called when the button is pressed
if((millis()-lastPressTime)>60){ // for debouncing
clockShown = false;
menuShown = true;
lcd.clear();
lastPressTime=millis();
}
}
and remove this part from your code : (the one in the loop() function)
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}

Rfid with Adafruit fingerprint together not working in Arduino Uno

I am trying to create an ATM module using Arduino. Im using a RFID reader to scan the card and Adafruit Fingerprint Module for fingerprint verification. The issue is both work but not together. Here is my code:
#include <Key.h>
#include <Keypad.h>
#include <Adafruit_Fingerprint.h>
#include <SD.h>
#include <SPI.h>
#include<SoftwareSerial.h>
SoftwareSerial mySerial(0, 1);
SoftwareSerial mySerials(2, 3);
int read_count = 0, tag_count = 0;
int j = 0, k = 0; // Variabvles to iterate in for loops
char data_temp, RFID_data[12], data_store[12];
boolean disp_control;
char filename[4], fileText[30], names[10];
int atm[5];
uint8_t n, num, p;
char key;
int p1, p2, p3, p4, z;
char fid;
File myFile;
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerials);
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, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypads = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
finger.begin(57600);
mySerial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
Serial.println("Please Place Your Card");
}
void RecieveData()
{
if (mySerial.available() > 0)
{
data_temp = mySerial.read();
RFID_data[read_count] = data_temp;
read_count++;
}
}
void ReadData()
{
if (read_count == 12) {
disp_control = true;
for (j = 0; j < 12; j++) {
data_store[j] = RFID_data[j];
}
int x = 0;
for (int i = 9; i < 12; i++) {
filename[x] = data_store[i];
x++;
}
filename[3] = '.';
filename[4] = 't';
filename[5] = 'x';
filename[6] = 't';
filename[7] = '\0';
myFile = SD.open(filename, FILE_READ);
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
for (int i = 0; i < 20; i++) {
fileText[i] = myFile.read();
}
int a, n = 0;
for (int i = 0; i < 4; i++) {
atm[a] = fileText[i];
a++;
}
for (int i = 5; fileText[i] != '\0'; i++) {
names[n] = fileText[i];
n++;
}
fid = fileText[4];
validate();
myFile.close();
}
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
read_count = 0;
tag_count++;
}
}
void validate() {
Serial.print("Hello ");
Serial.print(names);
Serial.println();
Serial.println();
Serial.println();
pin();
}
void pin() {
Serial.println("Please Enter Your ATM PIN");
p1 = keypads.getKey();
while (p1 == NO_KEY) {
p1 = keypads.getKey(); //UPDATE VALUE
}
Serial.print(p1 - 48);
p2 = keypads.getKey();
while (p2 == NO_KEY) {
p2 = keypads.getKey(); //UPDATE VALUE
}
Serial.print(p2 - 48);
p3 = keypads.getKey(); //UPDATE VALUE
while (p3 == NO_KEY) {
p3 = keypads.getKey(); //UPDATE VALUE
}
Serial.print(p3 - 48);
p4 = keypads.getKey(); //UPDATE VALUE
while (p4 == NO_KEY) {
p4 = keypads.getKey(); //UPDATE VALUE
}
Serial.print(p4 - 48);
Serial.println();
if ((p1 == fileText[0]) && (p2 == fileText[1]) && (p3 == fileText[2]) && (p4 == fileText[3])) {
Serial.println("Please verify Fingerprint");
}
else {
Serial.println("Invalid. Buzzer!!!!");
}
}
uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println("No finger detected");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return p;
default:
Serial.println("Unknown error");
return p;
}
// OK success!
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;
}
// OK converted!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return p;
} else {
Serial.println("Unknown error");
return p;
}
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
// found a match!
/*z = finger.fingerID;
if ((fid - 48) == z) {
Serial.println("Success");
}
else {
Serial.println("Fail");
}*/
}
void loop() {
RecieveData();
ReadData();
getFingerprintIDez();
delay(50);
}
If I initialize in this format, then fingerprint works:
mySerial.begin(9600);
finger.begin(57600);
Serial.begin(9600);
And if i do it this way, then RFID works:
finger.begin(57600);
mySerial.begin(9600);
Serial.begin(9600);
I want both of them to work i.e; I first Scan the Card, then verify pin and then it checks the validity of the fingerprint. But the issue is only one of them works at a time. If RFID works, then fingerprint doesn't even blink and if Fingerprint works, RFID doesn't read.
I'm new to this and I don't know where I am going wrong.

Arduino 16-step sequencer - Notes not in sync

I am using the following code for my arduino mega to send midi clock out to my drum machine & synth.
The problem appears when I also try to send midi notes exactly on the 'beat'.
Only the first note is perfectly synced, the rest of them are out of sync and you can hear it.
Is there any apparent flaw in the code?
// METRONOME
#include <MIDI.h>
#define LED1PIN 13
#define LED4PIN 7
#define SWITCHAPIN 5
#define SWITCHBPIN 2
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
void setup() {
pinMode(LED1PIN, OUTPUT);
pinMode(LED4PIN, OUTPUT);
pinMode(SWITCHAPIN, INPUT);
pinMode(SWITCHBPIN, INPUT);
digitalWrite(SWITCHAPIN, HIGH);
digitalWrite(SWITCHBPIN, HIGH);
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial.begin(9600);
Serial.println("Setting up");
}
unsigned long nextClockTime = 0;
int clockDelay = 20;
int tickCount = 0;
byte running = 0;
int pula = 0;
int currentStep = 1;
int lastPulse = 0;
int currPulse = 0;
int nextTickCount = 0;
typedef struct {
int noteNumber;
int velocity;
int noteLength;
bool enabled;
}
Step;
Step stepData[100];
void loop() {
stepData[0].enabled = true;
stepData[0].velocity = 127;
stepData[0].noteNumber = 40;
stepData[23].enabled = true;
stepData[23].velocity = 127;
stepData[23].noteNumber = 40;
stepData[47].enabled = true;
stepData[47].velocity = 127;
stepData[47].noteNumber = 40;
stepData[71].enabled = true;
stepData[71].velocity = 127;
stepData[71].noteNumber = 40;
stepData[95].enabled = true;
stepData[95].velocity = 127;
stepData[95].noteNumber = 40;
MIDI.read();
unsigned long milliseconds = millis();
if (milliseconds > nextClockTime) {
if (running)
MIDI.sendRealTime(MIDI_NAMESPACE::Clock);
nextClockTime = milliseconds + clockDelay;
nextTickCount = tickCount + 1;
if (tickCount == 0) {
if (stepData[tickCount].enabled) {
MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
}
}
if (tickCount == 23) {
if (stepData[tickCount].enabled) {
MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
currPulse = millis(); //1500
Serial.println(currPulse - lastPulse); //1500-500
lastPulse = currPulse;
}
}
if (tickCount == 47) {
if (stepData[tickCount].enabled) {
MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
currPulse = millis();
Serial.println(currPulse - lastPulse);
lastPulse = currPulse;
}
}
if (tickCount == 71) {
if (stepData[tickCount].enabled) {
MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
currPulse = millis();
Serial.println(currPulse - lastPulse);
lastPulse = currPulse;
}
}
if (tickCount == 95) {
if (stepData[tickCount].enabled) {
//MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
currPulse = millis();
Serial.println(currPulse - lastPulse);
lastPulse = currPulse;
}
nextTickCount = 0;
}
tickCount = nextTickCount;
} else if (digitalRead(SWITCHBPIN) == LOW) {
if (running) {
MIDI.sendRealTime(MIDI_NAMESPACE::Stop);
running = 0;
digitalWrite(LED4PIN, LOW);
}
} else {
clockDelay = analogRead(A0) / 10;
}
if (pula == 0) {
if (!running) {
MIDI.sendRealTime(MIDI_NAMESPACE::Start);
tickCount = 0;
running = 1;
digitalWrite(LED4PIN, HIGH);
pula = 1;
}
}
}
The beats are not evenly spaced. 24 - 1 is 23, 48 - 24 is 24 etc.
A simple fix would be to put your first beat on 0 not 1. Instead of incrementing tickCount, set a variable nextTickCount to tickCount +1, and assign it to tickCount at the end of the if (milliseconds > nextClockTime) { conditional bracket.
Also, set nextTickCount to 0 when tickCount reaches 95, but don't play any note (it will be played on the 0 beat )

Sending message to the extracted number

Please someone let me know why this code is not working. I want to extract message sender's number and then forward message to it using AT commands. It extracts the number of sender and stores it in a variable but why won't it send a message to that number?
#include <GSM.h>
GSM_SMS sms;
char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;
char number1[12] = "xxxxxxxxxx";
String number;
char inchar;
char outString[22];
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial1.print("ATE0\r");
Serial1.print("AT\r");
Serial1.print("AT+CMGF=1\r");
Serial1.print("AT+CNMI=1,2,0,0,0\r");
delay(1000);
}
void loop()
{
recSms();
}
void recSms()
{
if(Serial1.available())
{
char data = Serial1.read();
if(data == '+'){RcvdCheck = 1;}
if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}
if(RcvdConf == 1)
{
if(data == '\n'){RcvdEnd++;}
if(RcvdEnd == 3){RcvdEnd = 0;}
RcvdMsg[index] = data;
index++;
if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
Serial.print("Mobile Number is: ");
for(int x = 4;x < 17;x++)
{
number+=RcvdMsg[x];
MsgMob[x-4] = RcvdMsg[x];
}
Serial.print(number);
Serial.println();
Serial.print("Message Text: ");
for(int x = 46; x < MsgLength; x++)
{
MsgTxt[x-46] = RcvdMsg[x];
inchar=MsgTxt[x-46];
}
Serial.print(inchar);
Serial.println();
RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
MsgMob[15];
MsgTxt[50];
MsgLength = 0;
Serial.flush();
Serial1.flush();
if(inchar == '#')
{
sendInfo();
}
}
}
}
}
void sendInfo()
{
Serial1.print("AA");
delay(1000); //delay of 1
Serial1.println("AT");
delay(1000);
Serial1.write("AT+CMGF=1\r\n"); //set GSM to text mode
delay(1000);
Serial1.write("AT+CPMS=\"SM\"\r\n"); //Preferred SMS Message Storage
delay(1000);
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
delay(1000);
Serial1.print("HI");
delay(1000);
Serial1.write(0x1A); // sends ctrl+z end of message
delay(1000);
Serial.println("sms sent ");
} //end sendInfo()
Okay so the problem seems to be with these lines:
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
But if we write the lines written below, program works just fine!
Serial1.write("AT+CMGS=\"");
Serial1.print(number);
Serial1.write("\"\r");

Arduino mega crashes while reading somewhat large .txt from SD

I have an Arduino mega with an SD Shield and a GSM shield. I'm trying to send one sms to 200 numbers from a text file on the SD card, but the Arduino reboots every time I try to send to over 100 numbers. It doesn't crash when I try to send to 70 numbers. I only read one number at a time so I don't understand the problem. I'm pretty new to Arduino programming.
Please help me this is for a tournament. Here's the code:
#include <avr/pgmspace.h>
#include <SPI.h>
#include <SD.h>
#include <GSM.h>
#define PINNUMBER ""
GSM gsmAccess;
GSM_SMS sms;
// GSM
boolean notConnected;
//char input;
//byte input2;
//char txtContent[200];
byte i = 1;
byte f = 0;
boolean sendit;
//char senderNumber[11];
const String stopp PROGMEM = "Stopp";
//SD
char numbers[11];
//char nmbr;
int l = 0;
//Optimizing
const char q PROGMEM = '\xe5'; // å
const char w PROGMEM = '\xe4'; // ä
const char e PROGMEM = '\xf6'; // ö
const char r PROGMEM = '\xc5'; // Å
const char t PROGMEM = '\xc4'; // Ä
const char y PROGMEM = '\xd6'; // Ö
void setup() {
// txtContent[0] = '\0';
i = 0;
pinMode(53, OUTPUT);
Serial.begin(115200);
// Serial.begin(4800);
Serial.println(F("Connecting to GSM..."));
notConnected = true;
while (notConnected) {
if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
notConnected = false;
}
else {
Serial.println(F("Not Connected"));
delay(1000);
}
}
Serial.println(F("GSM Ready"));
if (!SD.begin(4)) {
Serial.println(F("Error with SD card"));
return;
}
Serial.println(F("SD Ready"));
delay(1000);
Serial.println(F("Write your sms and end it with * and press Send"));
Serial.flush();
}
void loop() {
char txtContent[300];
if (Serial.available() > 0 && !sendit) {
byte input2;
input2 = (int)Serial.read();
txtContent[i] = (char)input2;
if (txtContent[i] == '{') {
txtContent[i] = q;
}
else if (txtContent[i] == '}') {
txtContent[i] = w;
}
else if (txtContent[i] == '|') {
txtContent[i] = e;
}
else if (txtContent[i] == '[') {
txtContent[i] = r;
}
else if (txtContent[i] == ']') {
txtContent[i] = t;
}
else if (txtContent[i] == ';') {
txtContent[i] = y;
}
i++;
if (input2 == '*') {
// Remove the * from text.
for (int j = 0; j < sizeof(txtContent); j++) {
if (txtContent[j] == '*') {
txtContent[j] = '\0';
//Serial.println(txtContent);
}
}
sendit = true;
Serial.flush();
}
else {
sendit = false;
Serial.flush();
}
}
else if (sendit && txtContent[0] != '\0') {
int txtCount = 0;
// else if(sendit){
Serial.println(F("Sending please wait..."));
// Serial.flush();
File myFile;
myFile = SD.open("numbers.txt");
char input;
if (myFile) {
// if (myFile.available()) {
while(myFile.available()){
input = (char)myFile.read();
if (input == ',') {
sms.beginSMS(numbers);
sms.print(txtContent);
// sms.beginSMS("0704941025");
// sms.print("yo");
delay(30);
sms.endSMS();
delay(30);
Serial.println(numbers);
Serial.flush();
// Serial.flush();
for (int j = 0; j < sizeof(numbers); j++) {
if (numbers[i] != '\0') {
numbers[j] = '\0';
}
}
f = 0;
}
else {
numbers[f] = input;
f++;
}
}
myFile.close();
Serial.println(F("All texts have been sent."));
Serial.flush();
} else {
Serial.println(F("error opening numbers.txt"));
}
for (int j = 0; j < sizeof(txtContent); j++) { // Clear text
txtContent[j] = '\0';
}
i = 0;
sendit = false;
}
else {
// delay(1000);
if (sms.available()) {
char senderNumber[11];
sms.remoteNumber(senderNumber, 11);
if (sms.peek() == 'S') {
Serial.print(F("Detta nummer har skickat Stopp: "));
Serial.println(senderNumber);
}
sms.flush();
Serial.flush();
// sendit = false;
}
}
}
Arduino is a strange ground for me but if that is anything like C, then i risk saying sizeof(numbers) will likely return 44, because you're asking for the size of an array in bytes, this probably means your for loop will iterate up to numbers[43] when numbers[] length is 11. Consider this instead:
for (int j = 0; j < sizeof(numbers)/sizeof(numbers[0]); j++) {
Same goes for all the other times you check for the size of an array in this code.
Also, do double check your textfile to ensure its really written in your expected format: 11 numbers then a comma, with no exception. You should maybe try to figure out if f went past 11 digits before hitting a comma and "Fail gracefully" with an error message.

Resources