I have worked on a sketch for my Arduino Micro (not Pro Micro) that includes 4 mini arcade buttons with integrated LEDs. When I upload my sketch without the LED logic, everything works correctly but when I upload the sketch with the LED logic, the button press is recognized but not the note on/off message.
Does anyone see what would stop this from working?
Original Sketch
#include "MIDIUSB.h"
#include "PitchToNote.h"
#define NUM_BUTTONS 4
const uint16_t button1 = 2;
const uint16_t button2 = 3;
const uint16_t button3 = 4;
const uint16_t button4 = 5;
const uint16_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4};
const byte notePitches[NUM_BUTTONS] = {pitchC2, pitchD2b, pitchD2, pitchE2b};
uint16_t notesTime[NUM_BUTTONS];
uint16_t pressedButtons = 0x00;
uint16_t previousButtons = 0x00;
uint16_t intensity;
void setup() {
for (int i = 0; i < NUM_BUTTONS; i++)
pinMode(buttons[i], INPUT_PULLUP);
}
void loop() {
readButtons();
playNotes();
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
void readButtons()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
if (digitalRead(buttons[i]) == LOW)
{
bitWrite(pressedButtons, i, 1);
delay(2);
}
else
bitWrite(pressedButtons, i, 0);
}
}
void playNotes()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
{
if (bitRead(pressedButtons, i))
{
bitWrite(previousButtons, i , 1);
noteOn(0, notePitches[i], 100);
MidiUSB.flush();
}
else
{
bitWrite(previousButtons, i , 0);
noteOff(0, notePitches[i], 0);
MidiUSB.flush();
}
}
}
}
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
LED Logic
#include "MIDIUSB.h"
#include "PitchToNote.h"
#define NUM_BUTTONS 4
const uint16_t button1 = 2;
const uint16_t button2 = 3;
const uint16_t button3 = 4;
const uint16_t button4 = 5;
const uint16_t buttonLed1 = 6;
const uint16_t buttonLed2 = 7;
const uint16_t buttonLed3 = 8;
const uint16_t buttonLed4 = 9;
const uint16_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4};
const uint16_t ButtonsLEDS[NUM_BUTTONS] = {buttonLed1, buttonLed2, buttonLed3, buttonLed4};
const byte notePitches[NUM_BUTTONS] = {pitchC2, pitchD2b, pitchD2, pitchE2b};
int lastpress = 0;
uint16_t notesTime[NUM_BUTTONS];
uint16_t pressedButtons = 0x00;
uint16_t previousButtons = 0x00;
uint16_t intensity;
void setup() {
for (int i = 0; i < NUM_BUTTONS; i++)
{
pinMode(buttons[i], INPUT_PULLUP);
}
for (int i = 0; i < NUM_BUTTONS; i++)
{
pinMode(ButtonsLEDS[i], OUTPUT);
}
}
void loop() {
playNotes();
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
void readButtons()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
if (digitalRead(buttons[i]) == LOW)
{
for (int x = 0; x < NUM_BUTTONS; x++)
{
if (lastpress != i) {
digitalWrite(ButtonsLEDS[x], LOW);
}
}
digitalWrite(ButtonsLEDS[i], !digitalRead(ButtonsLEDS[i]));
lastpress = i;
bitWrite(pressedButtons, i, 1);
delay(2);
while (digitalRead(buttons[i]) == LOW) {
}
}
else
bitWrite(pressedButtons, i, 0);
}
}
void playNotes()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
{
if (bitRead(pressedButtons, i))
{
bitWrite(previousButtons, i , 1);
noteOn(0, notePitches[i], 100);
MidiUSB.flush();
}
else
{
bitWrite(previousButtons, i , 0);
noteOff(0, notePitches[i], 0);
MidiUSB.flush();
}
}
}
}
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
Related
I am using a arduino uno in an attempt to power two servos. Servo A should open for a couple seconds every 24 hours OR if button A is pressed. Servo B should open for a couple seconds if the water level sensor reads below 300 OR if button B is pressed. The code below works fine when I only include one servo, but adding the code for servo B ruins everything.
#include <Servo.h>
Servo myservoA;
Servo myservoB;
const int BUTTONA_PIN = 8;
const int BUTTONB_PIN = 6;
const int SERVOA_PIN = 9;
const int SERVOB_PIN = 7;
unsigned long dayTimer_ms = 0;
unsigned long autoOpenDelay_ms = 86400000;
int angle = 0;
int waterSensor = A0;
int waterLevel = 0;
void setup(){
myservoA.attach(SERVOA_PIN);
myservoB.attach(SERVOB_PIN);
pinMode(BUTTONA_PIN, INPUT_PULLUP);
pinMode(BUTTONB_PIN, INPUT_PULLUP);
myservoA.write(0);
myservoB.write(0);
}
void loop() {
if(millis() - dayTimer_ms > autoOpenDelay_ms)
{
dayTimer_ms = millis();
myservoA.write(180); //(open?)
delay(8000);
myservoA.write(0);
}
if(millis()<dayTimer_ms)//overflow handling (in case this runs for more than 50 days straight)
{
dayTimer_ms = millis();
}
if (!digitalRead(BUTTONA_PIN) && angle != 180)
{
angle = 180;
myservoA.write(angle);
}
if (digitalRead(BUTTONA_PIN) && angle != 0)
{
angle = 0;
myservoA.write(angle);
}
if (!digitalRead(BUTTONB_PIN) && angle != 180)
{
angle = 180;
myservoB.write(angle);
}
if (digitalRead(BUTTONB_PIN) && angle != 0)
{
angle = 0;
myservoB.write(angle);
}
int waterLevel = analogRead(waterSensor);
if (waterLevel <= 300){
myservoB.write(180);
delay(8000);
myservoB.write(0);
}
}
You are actually super close!
You just need to make a second variable to track angleA separately from angleB. So you should initialize another variable at the top "angleB" to zero and then replace "angle" with this new variable everywhere below line 52
#include <Servo.h>
Servo myservoA;
Servo myservoB;
const int BUTTONA_PIN = 8;
const int BUTTONB_PIN = 6;
const int SERVOA_PIN = 9;
const int SERVOB_PIN = 7;
unsigned long dayTimer_ms = 0;
unsigned long autoOpenDelay_ms = 86400000;
int angleA = 0;
int angleB = 0;
int waterSensor = A0;
int waterLevel = 0;
void setup(){
myservoA.attach(SERVOA_PIN);
myservoB.attach(SERVOB_PIN);
pinMode(BUTTONA_PIN, INPUT_PULLUP);
pinMode(BUTTONB_PIN, INPUT_PULLUP);
myservoA.write(0);
myservoB.write(0);
}
void loop() {
if(millis() - dayTimer_ms > autoOpenDelay_ms)
{
dayTimer_ms = millis();
myservoA.write(180); //(open?)
delay(8000);
myservoA.write(0);
angleA = 0;
}
if(millis()<dayTimer_ms)//overflow handling (in case this runs for more than 50 days straight)
{
dayTimer_ms = millis();
}
if (!digitalRead(BUTTONA_PIN) && angleA != 180)
{
angleA = 180;
myservoA.write(angleA);
}
if (digitalRead(BUTTONA_PIN) && angleA != 0)
{
angleA = 0;
myservoA.write(angleA);
}
if (!digitalRead(BUTTONB_PIN) && angleB != 180)
{
angleB = 180;
myservoB.write(angleB);
}
if (digitalRead(BUTTONB_PIN) && angleB != 0)
{
angleB = 0;
myservoB.write(angleB);
}
int waterLevel = analogRead(waterSensor);
if (waterLevel > 300 && angleB != 0)
{
myservoB.write(0);
angleB = 0;
}
else if (waterLevel < 200 && angleB != 180){
myservoB.write(180);
angleB = 180;
}
}
the bottom two levels may need to be adjusted (200 and 300). Basically this creates a slight de-bounce. By separating the levels at which the water turns on and turns off, it keeps the system from jittering right at the limit (constantly turning on and off). That being said, depending on the accuracy of your sensor and how important precise level control is in your application, you may want to tighten or move these values around a bit.
I wrote code to switch color on a LED strip depending on a value that I set (code below).
When I tried to load the same code on a NodeMCU ESP8266, this code doesn't work anymore. I used the library Adafruit Neopixel, which I thought is supported by the mentioned board.
Any help and/or advice is really welcome.
#include <Adafruit_NeoPixel.h>
//#define ATTINY
#define DIMCOLORE 3
#define NUMCOLORI (sizeof(colori) / ((DIMCOLORE)*sizeof(byte)))
#define DELTRANSIZIONE (tempoTransizione) / (numOfPixels)
#define SPENTO 0
#define ROSSO 1
#define VERDE 2
#define BLU 3
#define GIALLO 4
#define CIANO 5
#define MAGENTA 6
#define BIANCO 7
#define CELESTINO 8
const byte pinLuci = 4;
const byte numOfPixels = 51;
const int tresh_1 = 10; // white
const int tresh_2 = 20; // blue
const int tresh_3 = 30; // yellow
const int tresh_4 = 40; // red
const unsigned long tempoCritico = 5000;
const unsigned long tempoLampeggio = 300;
const unsigned long tempoTransizione = 270;
byte colori[][DIMCOLORE] = {
{ 0, 0, 0}, // 0 OF
{255, 0, 0}, // 1 ROSSO
{ 0, 255, 0}, // 2 VERDE
{ 0, 0, 255}, // 3 BLU
{255, 130, 0}, // 4 GIALLO
{ 0, 255, 255}, // 5 CIANO
{255, 0, 255}, // 6 MAGENTA
{255, 130, 50}, // 7 BIANCO
};
unsigned long inizioAlto = 0;
byte colore = BIANCO;
byte ultimoColore = colore;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numOfPixels, pinLuci, NEO_GRB + NEO_KHZ800);
void impostaColore(byte indiceColore) {
for (byte i = 0; i < numOfPixels; i++) pixels.setPixelColor(i, pixels.Color(colori[indiceColore][0], colori[indiceColore][1], colori[indiceColore][2]));
pixels.show();
}
void impostaColore(byte valR, byte valG, byte valB) {
for (byte i = 0; i < numOfPixels; i++) pixels.setPixelColor(i, pixels.Color(valR, valG, valB));
pixels.show();
}
void impostaSequenza(byte indiceColore) {
for (byte i = 0; i < numOfPixels; i++) {
pixels.setPixelColor(i, pixels.Color(colori[indiceColore][0], colori[indiceColore][1], colori[indiceColore][2]));
pixels.show();
delay(DELTRANSIZIONE);
}
}
void impostaSequenza(byte valR, byte valG, byte valB) {
for (byte i = 0; i < numOfPixels ; i++) {
pixels.setPixelColor(i, pixels.Color(valR, valG, valB));
pixels.show();
delay(DELTRANSIZIONE);
}
}
void lampeggia(byte indiceColore, byte volte, unsigned long tempoLamp) {
for (byte i = 0 ; i < volte ; i++) {
impostaColore(0);
delay(tempoLamp);
impostaColore(indiceColore);
delay(tempoLamp);
}
}
void setup() {
#ifndef ATTINY
Serial.begin(115200);
#endif
pixels.begin();
for (byte i = 0; i < 54; i++) {
pixels.setPixelColor(i, pixels.Color(colori[2][0], colori[2][1], colori[2][2]));
pixels.show();
}
for (byte j = 0 ; j <= NUMCOLORI ; j++) {
impostaColore(j);
delay(50);
}
impostaColore(BIANCO);
}
void loop() {
int value = 5;
#ifndef ATTINY
Serial.print(millis());
Serial.print('\t');
Serial.print(value);
#endif
if (value < tresh_1) {
#ifndef ATTINY
Serial.print(" 0 ");
#endif
inizioAlto = 0;
colore = BIANCO;
} else if ((value >= tresh_1) && (value < tresh_2) && (value < tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" BASSA ");
#endif
inizioAlto = 0;
colore = BLU;
} else if ((value >= tresh_2) && (value < tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" MEDIA ");
#endif
inizioAlto = 0;
colore = GIALLO;
} else if ((value >= tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" ALTA ");
#endif
inizioAlto = 0;
colore = ROSSO;
} else if (value >= tresh_4) {
#ifndef ATTINY
Serial.print(" MAX ");
#endif
if (inizioAlto == 0) {
#ifndef ATTINY
Serial.print(" ----- ");
#endif
inizioAlto = millis();
colore = ROSSO;
} else if ((millis() - inizioAlto) > tempoCritico) {
#ifndef ATTINY
Serial.print(" +++++ ");
#endif
lampeggia(ROSSO, 1, tempoLampeggio / 2);
}
}
if (colore != ultimoColore) {
impostaSequenza(colore);
ultimoColore = colore;
}
#ifndef ATTINY
Serial.println();
#endif
delay(tempoLampeggio / 2);
}
The unique line I changed it is about the pin name:
const byte pinLuci = D5;
I attached some photos about the hardware connection I made, maybe the problem could be there.
I want to make clear that with Arduino Uno everything works fine.
If the error is that i does not compile then post the error from the console.
Else it is properly the wrong pin you are using.
The pin numbers and GPIO numbers isn't the same.
If you want to use GPIO04 you should use pin D2 in your sketch. See the following pin mapping
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
This may seem like a foolish problem and maybe its description is not the best I could have devised.
I am making a velocity sensor that uses two IR beams to calculate velocity based on the time it takes to break both beams.
I have two testing methods.
My hand (5-10 m/s)
A high speed cannon (30-60 m/s).
I have ruled out that it's a problem with the signal from the IR beams with an oscilloscope, when the code fails/works the data is identical on the scope.
My problem is that my code works when I use my hand, but still irregularly fails, while it fails more often at high speed. All the conditions are the same in both scenarios. What could be the issue?
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
const int rs = 9, en = 8, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
File root;
int fileNo = 0;
String currentFileName;
const int CS = 10;
const byte interruptPinStart = 2;
const byte interruptPinFinish = 3;
volatile unsigned long int startTimeMillis = 0;
volatile unsigned long int stopTimeMillis = 0;
volatile unsigned long int startTimeMicros = 0;
volatile unsigned long int stopTimeMicros = 0;
volatile unsigned long int microsDifference = 0;
volatile unsigned long int millisDifference = 0;
int launchNo = 0;
float currentVelocity = 0;
volatile boolean started = false;
String inputString = "";
boolean stringComplete = false;
const int txLed1 = 14;
const int statusLed1 = 15;
const int statusLed2 = 16;
volatile boolean triggerDone = false;
float velocity = 0;
String temp;
unsigned long int lockout = 0;
boolean lockedOut = false;
boolean fileFail = false;
int testNo = 0;
void setup() {
inputString.reserve(200);
pinMode(statusLed1, OUTPUT);
pinMode(statusLed2, OUTPUT);
pinMode(txLed1, OUTPUT);
Serial.begin(9600);
while (!Serial) {
;
}
lcd.begin(16, 2);
pinMode(interruptPinStart, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPinStart), startTrigger, RISING);
pinMode(interruptPinFinish, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPinFinish), stopTrigger, RISING);
Serial.print("Initializing SD card...");
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
root = SD.open("/");
newDirectory(root);
Serial.println("done!");
lcd.clear();
lcd.print(currentFileName);
tone(txLed1, 38000);
}
void loop() {
int millsDiff = millis() - stopTimeMillis;
if (triggerDone) {
lockedOut = true;
Serial.print("Micros Diffrence: ");
Serial.println(microsDifference);
Serial.print("Millis Difference: ");
Serial.println(millisDifference);
float millDiff = (float) millisDifference;
float microDiff = (float) microsDifference;
if (microDiff > 0) {
velocity = (float) 0.09 / (microDiff/1000000);
testNo++;
temp = String(launchNo) + "%" + String(microsDifference) + "%" + String(velocity);
if (velocity > 10.0) {
root = SD.open(currentFileName, FILE_WRITE);
if (root) {
root.println(temp);
root.close();
Serial.println(temp);
launchNo++;
} else {
Serial.println("error opening file, " + currentFileName);
fileFail = true;
}
}
if (fileFail) {
lcd.clear();
lcd.print("File Error");
lcd.setCursor(0, 1);
lcd.print("Vel " + String(launchNo) + ": " + String(velocity) + " m/s");
fileFail = false;
} else {
lcd.clear();
lcd.print("Test Number: " + String(testNo));
lcd.setCursor(0, 1);
lcd.print("Vel " + String(launchNo) + ": " + String(velocity) + " m/s");
}
}
triggerDone = false;
Serial.println("Test Number: " + String(testNo));
}
if (digitalRead(interruptPinStart) == LOW) {
digitalWrite(statusLed1, HIGH);
} else {
digitalWrite(statusLed1, LOW);
}
if (digitalRead(interruptPinFinish) == LOW) {
digitalWrite(statusLed2, HIGH);
} else {
digitalWrite(statusLed2, LOW);
}
}
void startTrigger() {
startTimeMicros = micros();
startTimeMillis = millis();
volatile int diff1 = startTimeMicros - startTimeMillis;
volatile int diff2 = startTimeMillis - stopTimeMillis;
if (diff2 > 200) {
if (started == false || diff1 > 1000) {
started = true;
triggerDone = false;
}
}
}
void stopTrigger() {
stopTimeMicros = micros();
stopTimeMillis = millis();
microsDifference = stopTimeMicros - startTimeMicros;
millisDifference = stopTimeMillis - startTimeMillis;
if ((millisDifference > 0 && millisDifference < 800) && started) {
microsDifference = stopTimeMicros - startTimeMicros;
millisDifference = stopTimeMillis - startTimeMillis;
started = false;
triggerDone = true;
}
}
I am a beginner with arduino and I'm trying to make a sinus wave generator. Since I've recently found I can't put everything into main void loop, I'm trying to use interrupts. I have problem with changing variable inside of the interrupt (Delay), I don't know where's the mistake.
Here is my code:
int sine256[] = { //256 sin values from 0 to 2pi
};
int i = 0;
int sensorPin = 7;
int outputPin = 6;
volatile float Delay = 10000;
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
pinMode(sensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(sensorPin), freq, RISING);
}
void loop()
{
analogWrite(6,sine256[i]);
i = i + 1;
if(i == 256){
i = 0;
}
Serial.println(Delay);
delayMicroseconds(Delay);
}
void freq() {
Delay = Delay/2;
}
EDIT
Try this:
int sine256[] = { //256 sin values from 0 to 2pi
};
int i = 0;
int sensorPin = 7;
int outputPin = 6;
volatile float Delay = 10000;
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
pinMode(sensorPin, INPUT);
//attachInterrupt(digitalPinToInterrupt(sensorPin), freq, RISING);
}
void loop()
{
analogWrite(6,sine256[i]);
i = i + 1;
if(i == 256){
i = 0;
}
Serial.println(Delay);
freq();
delay(Delay);
}
void freq() {
Delay = Delay / 2;
}
https://www.arduino.cc/en/Reference/AttachInterrupt
Try taking a look at that.
What model are you using?
The only thing that causes me troubles now is the button; when i press it, it often respond as if i had pressed the button multiple times (2,3 or 4x).
This is my final code for now. Since the execution time for a void loop is 12 microseconds, i've calculated delay required to run a generator on 20,40 & 60Hz.
int sine256[] = { //256 sin values from 0 to 2pi (from 0 to 255)
int i = 0;
int sensorPin = 2;
volatile int outputPin = 7;
volatile float Delay = 1000;
int time1;
int time2;
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
pinMode(sensorPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(sensorPin), freq, FALLING);
}
void loop()
{
//time1 = micros();
analogWrite(outputPin,sine256[i]);
i = i + 1;
if(i == 256){
i = 0;
}
//time2 = micros();
//Serial.println(time2 - time1);
delay(Delay);
}
void freq() {
outputPin = 6;
if(Delay == 0.02){
analogWrite(6,LOW);
outputPin = 7;
Delay = 1000;
}
if(Delay == 0.04){
Delay = 0.02;
}
if(Delay == 0.09){
Delay = 0.04;
}
if((Delay == 1000)&&(outputPin == 6)){
Delay = 0.09;
}
Serial.println(Delay);
}
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.