I'm doing a project with an arduino, a gps module, a microSD module and a bluetooth module.
There is also 2 buttons for the project, one is in charge of sending coordinates to the microSD card and the other one through bluetooth. The first button, the one for microSd card, it has 3 functions: Start, pause, and stop. This is where I need some guidance please!
I'm confused as to how to make a button to pause, and either wait for the start of stop command.
This code is checking buttn1 and 2. If button 1 is pressed, then there will be 3 cases (plus default). This is where I need some suggestions. Please help! How do I make it to where case 2 pauses the loop, and case 3 would I need to add an empty loop too?
thanks
Here is my code so far:
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#define button1 7
//#define button2
#define redLED 4
#define greenLED 3
//Pins
const int chipSelect = 10; // setup SD Card Chip Select Pin
int gpsTxPin = A0;
int gpsRxPin = A1;
SoftwareSerial gpsSerial(gpsTxPin, gpsRxPin);
TinyGPSPlus gps;
int state = 0;
int old = 0;
int buttonPoll = 0;
int buttonPoll2 = 0;
pinMode(LED,OUTPUT);
void setup() {
pinMode(button, INPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED,OUTPUT);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
Serial.begin(9600);
gpsSerial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed!");
digitalWrite(redLED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(redLED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
return;
}
Serial.println("card initialized.");
}
void loop() {
CheckButton1();
CheckButton2();
}
void CheckButton1()
{
buttonPoll = digitalRead(button1); //poll the state of button
if buttonPoll == 1){
delay(50);
buttonPoll = digitalRead(button1); //poll button again
if(buttonPoll ==0) { //if it's 0 considered one press
state = old + 1; //incrase stte by 1
}}
else{
delay(100);
}
switch (state) {
case 1: //Start
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
old = state;
File dataFile = SD.open("GPSdata.txt", FILE_WRITE);
// Read GPS Data
while(gpsSerial.available()>0){
gps.encode(gpsSerial.read());
if (gps.location.isUpdated()){
dataFile.print("Latitude= ");
dataFile.print(gps.location.lat(), 6);
dataFile.print(" Longitude= ");
dataFile.println(gps.location.lng(), 6);
}dataFile.close();
break;
case 2: //Pause
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
old = state;
break;
case 3: //Stop
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
old = state;
break;
default;
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
old = 0;
break;
}
}
void CheckButton2()
{
while (digitalRead(button2) == HIGH){
while(gpsSerial.available()>0){
gps.encode(gpsSerial.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
}
}
else
digitalWrite(greenLED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(greenLED, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}