Hi all, first post here, read the rules and searched through so many threads with no luck, so I am desperate.
What I need is pretty straightforward: I want to start and stop a loop with the click of a button.
Example:
- Arduino on, setup code has run, loop code not running.
- Press button, release button.
- Loop starts and repeats until I want to stop.
- Press button, release button.
- Loop stops.
- Repeat.
Every example I see on here is mainly focused on turning an LED or motor on or off, which is simple enough. I can't find anything to actually run a program as there aren't really "state changes" and I don't have LEDs. (EDIT: yes I realize this was stupid, I couldn't figure out how to word "simple state changes like HIGH and LOW"...)
This is for a temperature data logger using a thermocouple module, I want to click a button to start the loop, record temps over a period of time, and stop the recording so I can remove the SD, review the temps on a graph and do it over and over.
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
#include <max6675.h>
const int chipSelect = 10;
int ktcSO = 7;
int ktcCS = 6;
int ktcCLK = 5;
int run;
int buttonPin;
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
File myFile;
RTC_DS1307 rtc;
void setup()
{
run = 0; //starts stopped
buttonPin = 9;
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
// setup for the RTC
while(!Serial);
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if(! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
// setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//open file
myFile=SD.open("Temperature_Logger.txt", FILE_WRITE);
// if the file opened ok, write to it:
if (myFile) {
Serial.println("File opened ok");
// print the headings for our data
myFile.println("Date, Time, Temperature ºC");
}
myFile.close();
}
void loggingTime()
{
DateTime now = rtc.now();
char dateBuffer[12];
sprintf(dateBuffer,"%02u-%02u-%04u ",now.month(),now.day(),now.year());
Serial.print(dateBuffer);
sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second());
Serial.println(dateBuffer);
myFile = SD.open("Temperature_Logger.txt", FILE_WRITE);
if (myFile) {
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print('/');
myFile.print(now.year(), DEC);
myFile.print(", ");
myFile.print(dateBuffer);
myFile.print(", ");
}
Serial.print(dateBuffer);
Serial.print(", ");
myFile.close();
delay(1000);
}
void loggingTemperature()
{
myFile = SD.open("Temperature_Logger.txt.txt", FILE_WRITE);
if (myFile) {
Serial.println("open with success");
Serial.print(ktc.readCelsius());
Serial.println(",");
myFile.print(ktc.readCelsius());
myFile.println(" ");
}
myFile.close();
}
void loop()
{
{
loggingTime();
loggingTemperature();
delay(1000);
}
}
loggerMay11.ino (2.35 KB)