Hy,
if i upload programs that ony handle one of the 2 fuctions it works fine but when i try to merge them none work at all. my program does respond to the serial.println functions and it reads the input without fault.
it's when it has to call or play audio it doesn't respond, then few minutes later it sends a bunch of jumbled characters back and the program just acts as if nothing happened.
It's urgent so please if you have a suggestion, don't hold back.
My code:
#include <TMRpcm.h>
#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#define BUTTON_CALL 5 // call
#define BUTTON_PLAY 6 // play
#define RELAY 8 // audio relais switch sim or pwm
#define RELAY_2 10 // audio relais switch headphone or speaker
#define SWITCH_PLAY 7 // to do
#define SD_CSPin 4
#define RELAY_INPUT A2
#define LED_BATTERY_100 A1 // extention
#define LED_BATTERY_75 A3 // extention
#define LED_BATTERY_50 A4 // extention
#define BATTERY_SENSE A0 // extentiongf
// call(2) & audioplay(1) button
int button_audio_state = 0;
int relay_switch_state = 0;
int button_call_state = 0;
int play_switch_state = 0;
int relay_switch_previous_state = 0;
int button_call__previous_state = 0;
int button_audio_previous_state = 0;
int play_switch_previous_state = 0;
int prevButton1Press = 0;
int prevButton2Press = 0;
int currButton1Press = 0;
int currButton2Press = 0;
int batteryValue = 0;
// sim number & pin setup
SoftwareSerial sim(3, 2);
String number = "+32499735146"; //-> change with your number
TMRpcm audio;
int file_number = 0;
char filePrefixname[50] = "rec";
char exten[10] = ".wav";
// const int recordLed = 5;
// const int mic_pin = A5;
// const int sample_rate = 16000;
int dt = 50;
int dl = 6000; // delay time equal to length of playMe file equal to audio file for constant looping // 60000 =1 minute
int _timeout;
String _buffer;
// delay function for with serial log.
void wait_min(int mins)
{
int count = 0;
int secs = mins * 60;
while (1)
{
Serial.print('.');
delay(1000);
count++;
if (count == secs)
{
count = 0;
break;
}
}
Serial.println();
return;
}
String _readSerial()
{
_timeout = 0;
while (!sim.available() && _timeout < 12000)
{
delay(13);
_timeout++;
}
if (sim.available())
{
return sim.readString();
}
}
void callMe()
{
sim.print(F("ATD"));
sim.print(number);
sim.print(F(";\r\n"));
_buffer = _readSerial();
Serial.println(_buffer);
}
void playMe()
{
audio.setVolume(5);
audio.play("info.wav");
// audio.play("Praatpaal.wav");
}
void BatteryLevel()
{
batteryValue = analogRead(A0);
if(batteryValue > 820)
{
digitalWrite(LED_BATTERY_100,HIGH);
digitalWrite(LED_BATTERY_75,HIGH);
digitalWrite(LED_BATTERY_50,HIGH);
}
if(batteryValue > 801)
{
digitalWrite(LED_BATTERY_100,LOW);
digitalWrite(LED_BATTERY_75,HIGH);
digitalWrite(LED_BATTERY_50,HIGH);
}
if(batteryValue > 782)
{
digitalWrite(LED_BATTERY_100,LOW);
digitalWrite(LED_BATTERY_75,LOW);
digitalWrite(LED_BATTERY_50,HIGH);
}
}
// 840 = 100% (4.1V ; 12,88V)
// (19) 820 = 83% (4V ; 12,57V )
// (19) 801 = 67% (3.91V ; 12,289V )
// 782 = 50% (3.81V ; 12V)
void setup()
{
pinMode(BUTTON_CALL, INPUT);
pinMode(BUTTON_PLAY, INPUT);
pinMode(RELAY_INPUT, INPUT);
pinMode(BATTERY_SENSE, INPUT);
pinMode(RELAY, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(LED_BATTERY_100, OUTPUT);
pinMode(LED_BATTERY_75, OUTPUT);
pinMode(LED_BATTERY_50, OUTPUT);
Serial.begin(9600);
int RelaySwitchState = 0;
}
void loop()
{
//BatteryLevel();
relay_switch_state = digitalRead(RELAY_INPUT);
// Check if the switch state has changed since the last loop iteration
if (relay_switch_state != relay_switch_previous_state)
{
// Update the previous switch state to the current state
relay_switch_previous_state = relay_switch_state;
if (relay_switch_state == HIGH)
{
digitalWrite(RELAY_2, HIGH); // Speaker
delay(300);
Serial.println("relay switch high");
}
if (relay_switch_state == LOW)
{
digitalWrite(RELAY_2, LOW); // headphone
delay(300);
Serial.println("relay switch not high");
}
}
play_switch_state = digitalRead(SWITCH_PLAY);
// Check if the switch state has changed since the last loop iteration
//if (play_switch_state != play_switch_previous_state)
//{
// Update the previous switch state to the current state
// play_switch_previous_state = play_switch_state;
if (play_switch_state == HIGH)
{
Serial.println("Info looping when no superviser");
digitalWrite(RELAY, HIGH); // switching to amp
delay(dt);
}
// Read current button press states
currButton1Press = digitalRead(BUTTON_CALL);
currButton2Press = digitalRead(BUTTON_PLAY);
if (currButton2Press != prevButton2Press)
{
// Update the previous switch state to the current state
prevButton2Press = currButton2Press;
}
// If button 2 is pressed and button 1 is not pressed
if (currButton2Press == LOW & currButton1Press ==HIGH)
{
Serial.println("Call button has been pressed");
digitalWrite(RELAY, HIGH); // switching to sim
delay(dt);
//callMe();
sim.print(F("ATD"));
sim.print(number);
sim.print(F(";\r\n"));
_buffer = _readSerial();
Serial.println(_buffer);
delay(dt);
}
// Check if the switch state has changed since the last loop iteration
if (currButton1Press != prevButton1Press)
{
// Update the previous switch state to the current state
prevButton1Press = currButton1Press;
}
// If button 1 is pressed and button 2 is not pressed
if (currButton1Press == LOW & currButton2Press == HIGH)
{
Serial.println("Info button has been pressed");
digitalWrite(RELAY, LOW); // switching to pwm
delay(dt);
audio.speakerPin = 9;
Serial.begin(9600);
// Sets up the pins
// pinMode(mic_pin, INPUT);
// pinMode(recordLed, OUTPUT);
Serial.println("loading... SD card");
if (!SD.begin(SD_CSPin))
{
Serial.println("An Error has occurred while mounting SD");
}
while (!SD.begin(SD_CSPin))
{
Serial.print(".");
delay(500);
}
audio.CSPin = SD_CSPin;
playMe();
delay(dl);
}
}
code for pwm .wav file play
/*
#include <SD.h>
#define SD_ChipSelectPin 10
#include <TMRpcm.h>
TMRpcm tmrpcm;
void setup(){
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
tmrpcm.setVolume (5);
Serial.println("start play");
tmrpcm.play("Test.wav");
}
void loop(){ }
*/
#include <SD.h> // need to include the SD library
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 4 //using digital pin 4 on arduino nano 328
#include <TMRpcm.h> // also need to include this library...
#include "SD.h"
#include "SPI.h"
#define BUTTON_PIN1 5 //play
TMRpcm tmrpcm; // create an object for use in this sketch
int button1press = 0;
void setup(){
pinMode(BUTTON_PIN1, INPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin))
{ // see if the card is present and can be initialized:
Serial.println("SD fail");}
else {
Serial.println("SD ok");
}
return; // don't do anything more if not
}
void loop(){
button1press=digitalRead(BUTTON_PIN1);
tmrpcm.setVolume(4);
if(button1press == LOW)
{
digitalWrite(8,HIGH);
digitalWrite(10,LOW);
delay(400);
Serial.println("info button pressed");
tmrpcm.play("Test.wav");
}
}
code for calling to a telephone number
code for calling to a telephone number
#include <SoftwareSerial.h>
#define BUTTON_PIN 6
SoftwareSerial sim(3, 2);
int _timeout;
String _buffer;
String number = "+32499735146"; //-> change with your number // maud vanh.
int button1press = 0;
int Relay_Switch_State =0;
void setup() {
//delay(7000); //delay for 7 seconds to make sure the modules get the signal
Serial.begin(9600);
_buffer.reserve(50);
Serial.println("System Started...");
sim.begin(9600);
delay(1000);
Serial.println("Ready to call");
pinMode(BUTTON_PIN, INPUT);
pinMode(8, OUTPUT);
digitalWrite(10,OUTPUT);
}
void loop() {
button1press=digitalRead(BUTTON_PIN);
if(button1press==LOW){
digitalWrite(8,LOW);
delay(100);
Serial.println("Button 1 has been Pressed");
callNumber();
// digitalWrite(10,HIGH);
delay(2000);
}
Relay_Switch_State = digitalRead(8);
if (Relay_Switch_State == HIGH)
{
digitalWrite(10,HIGH);//PWM
delay(1000);
Serial.println("relay switch high");
delay(100);
}
if(Relay_Switch_State == LOW)
{
digitalWrite(10,LOW); //headphone
delay(1000);
Serial.println("relay switch not high");
delay(100);
}
}
String _readSerial() {
_timeout = 0;
while (!sim.available() && _timeout < 12000 )
{
delay(13);
_timeout++;
}
if (sim.available()) {
return sim.readString();
}
}
void callNumber() {
sim.print (F("ATD"));
sim.print (number);
sim.print (F(";\r\n"));
_buffer = _readSerial();
Serial.println(_buffer);
}
In the first function, the delay(400) seems only to be needed to debounce a pushbutton.
Add real button debouncing and state change detection to the loop() in the first function.
See the IDE
Files/Examples/0.2Digital/StateChangeDetection
Get that function working without any delay() calls.
Second.
In the second function, the loop() is amenable to being expressed as a finitestatemachine, or FSM, so that rather than rely on delays for programming flow, you restructure the code to take tiny steps towards whatever is its current goal. In between those steps control is relinquished so the processor can pay attention to other tasks, which will also be best expressed as FSMs or at least be such that they do not "block" and hog the processor.
Get the second function working as a non-blocking process.
You may wish to google and see what I am talking about, I suggest three searches and some time spent:
arduino blink without delay
and
arduino two things at once
and
arduino finite state machine
Once both sketches are purged of delays and blocking, it will be easier to comb them for your full functionality.
alright got it kinda to work, i can call but, playing audio is still something he just doesn't want to do.
i've read it could be a digitalread() function, but my test program i also use this function and it works fine. I'm out of guesses all help is appreciated.
My updated code: ` #include <SoftwareSerial.h> #define BUTTON_PIN 6//call #define BUTTON_PIN2 5// play #include <SD.h> // need to include the SD library
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560 #define SD_ChipSelectPin 4 //using digital pin 4 on arduino nano 328 #include <TMRpcm.h> // also need to include this library... #include "SD.h" #include "SPI.h"
TMRpcm tmrpcm; // create an object for use in this sketch
SoftwareSerial sim(3, 2);
int _timeout;
String _buffer;
String number = "+32499735146 "; //-> change with your number // maud vanh.
int button1press;
int button2press;
int Relay_Switch_State = 0;
int relay_switch_previous_state = 0;
void setup() {
tmrpcm.speakerPin = 9;
delay(5000); //delay for 5 seconds to make sure the modules get the signal
Serial.begin(9600);
_buffer.reserve(50);
Serial.println("System Started...");
delay(1000);
Serial.println("Ready to do stuff");
pinMode(BUTTON_PIN, INPUT);
pinMode(BUTTON_PIN2, INPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
pinMode(A2,INPUT);
}
void loop() {
button1press=digitalRead(BUTTON_PIN);
if(button1press==LOW){
digitalWrite(8,LOW);
delay(100);
Serial.println("Button 1 has been Pressed");
callNumber();
delay(100);
}
button2press=digitalRead(BUTTON_PIN2);
if(button2press==LOW){
digitalWrite(8,HIGH);
delay(100);
Serial.println("Button 2 has been Pressed");
playAudio();
delay(1000);
}
Relay_Switch_State = digitalRead(A2);
// Check if the switch state has changed since the last loop iteration
if (Relay_Switch_State != relay_switch_previous_state)
{
// Update the previous switch state to the current state
relay_switch_previous_state = Relay_Switch_State;
if (Relay_Switch_State == HIGH)
{
digitalWrite(10, HIGH); // Speaker
delay(300);
Serial.println("relay switch high");
}
if (Relay_Switch_State == LOW)
{
digitalWrite(10, LOW); // headphone
delay(300);
Serial.println("relay switch not high");
}