#define SENSOR 2 // define pint 2 for sensor
#define ACTION 9 // define pin 9 as for ACTION
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
File myFile;
const int chipSelect = 4;
SoftwareSerial BTserial(0, 1);
LiquidCrystal_I2C lcd(0x27,20, 4);
float value=0;
float rev=0;
int rpm;
int spd;
int oldtime=0;
int time;
int mnt;
int met;
int Count = digitalRead(ACTION);
void isr()
{
rev++;
}
void cnt() //count
{
Count++;
}
int inPinA = 8;
int inPinB = 7;
int inPinC = 6;
int inPinD = 5;
int outPin = 13;
int readingA;
int readingB;
int readingC;
int readingD;
int n = 39;
int debounce=200;
int previous;
int ttm;
float Call;
float ccnt;
int readdata = 0;
void setup() {
pinMode(SENSOR, INPUT_PULLUP);
pinMode(ACTION, OUTPUT);
attachInterrupt(0,isr,RISING); //attaching the interrupt
attachInterrupt(1,cnt,FALLING); //attaching the interrupt
BTserial.begin(38400);
pinMode(inPinA, INPUT);
pinMode(inPinB, INPUT);
pinMode(inPinC, INPUT);
pinMode(inPinD, INPUT);
pinMode(outPin, OUTPUT);
lcd.begin();
lcd.setCursor(0,0);
lcd.print("NERASUAN UNIVERSITY");
delay(1500);
lcd.setCursor(5,2);
lcd.print("ELECTRICAL");
lcd.setCursor(0,3);
lcd.print("ENGINEERING PROJECTS");
delay(2500);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("PLEASE SELECT YOUR ");
lcd.setCursor(0,1);
lcd.print("WEIGHT, PRESS BUTTON");
lcd.setCursor(0,3);
lcd.print("BETWEEN 40 TO 90 Kg");
readdata = 1;
}
void Setdata()
{
readingA = digitalRead(inPinA);
readingB = digitalRead(inPinB);
readingC = digitalRead(inPinC);
if (readingA == HIGH && millis() - time > debounce ) //A_______________________________________
{
lcd.clear();
time=millis();
n = n+1;
if (n >= 90 )
{
n = 90;
}
lcd.setCursor(5,0);
lcd.print("YOUR WEIGHT");
lcd.setCursor(5,2);
lcd.print(n);
lcd.setCursor(8,2);
lcd.print("KILOGRAM");
}
if (readingB == HIGH && millis() - time > debounce) //B_________________________________________
{
lcd.clear();
time = millis();
n = n-1;
if (n <= 40 )
{
n = 40;
}
lcd.setCursor(5,0);
lcd.print("YOUR WEIGHT");
lcd.setCursor(5,2);
lcd.print(n);
lcd.setCursor(8,2);
lcd.print("KILOGRAM");
digitalWrite(outPin, n);
previous = readingA;
}
if (readingC == HIGH && millis() - time > debounce) //C ______________________________________________
{
lcd.clear();
time = millis();
lcd.setCursor(0,0);
lcd.print("YOUR SELECT WEIGHT");
lcd.setCursor(5,2);
lcd.print(n);
lcd.setCursor(8,2);
lcd.print("KILOGRAM");
delay(2000);
lcd.setCursor(8,3);
lcd.print("START IN");
lcd.setCursor(18,3);
lcd.print("3");
delay(1250);
lcd.setCursor(18,3);
lcd.print("2");
delay(1250);
lcd.setCursor(18,3);
lcd.print("1");
delay(1250);
lcd.clear();
readdata = 0;
}
}
void BTandCal()
{
readingA = digitalRead(inPinA);
mnt++;
//------------------------------------------ SPEED
delay(1000);
detachInterrupt(0); //detaches the interrupt
time=millis()-oldtime; //finds the time
rpm=(rev/time)*60000; //calculates rpm
spd=rpm*0.10472*0.3*5; //calculate speed
oldtime=millis(); //saves the current time
rev=0;
int sspd=spd/2;
float ccnt=Count*2*3.14159*0.3;
int ttm=mnt/60;
float call;
if (sspd >= 0 && sspd < 10)
{
met = 3.2;
}
if (sspd >= 10 && sspd < 15)
{
met = 4.8;
}
if (sspd >= 15 && sspd < 20)
{
met = 5.9;
}
if (sspd >= 20 && sspd < 25)
{
met = 7.1;
}
if (sspd >= 25 && sspd < 30)
{
met = 8.4;
}
if (sspd ==0 )
{
if (ccnt == 0)
{
call = 0;
}
else
{
call = call;
}
}
else
{
call=0.0175*n*mnt*met/60;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SPEED");
lcd.setCursor(10,0);
lcd.print(sspd);
lcd.setCursor(15,0);
lcd.print("KM/HR");
attachInterrupt(0,isr,RISING);
//------------------------------------------ ROUND
lcd.setCursor(0,1);
lcd.print("DISTANCE");
lcd.setCursor(9,1);
lcd.print(ccnt);
lcd.setCursor(18,1);
lcd.print("M");
//-------------------------------------------- TIME
lcd.setCursor(0,2);
lcd.print("TIME");
lcd.setCursor(10,2);
lcd.print( ttm);
lcd.setCursor(17,2);
lcd.print("MIN");
//-------------------------------------------- CALORIES BURN
lcd.setCursor(0,3);
lcd.print("CALORIES");
lcd.setCursor(10,3);
lcd.print( call);
lcd.setCursor(16,3);
lcd.print("KCAL");
BTserial.print("<");
BTserial.print(sspd);
BTserial.print(",");
BTserial.print(ccnt);
BTserial.print(",");
BTserial.print(ttm);
BTserial.print(",");
BTserial.print(call);
BTserial.print(">");
if (readingD == HIGH && millis() - time > debounce)
{
lcd.clear();
time = millis();
lcd.setCursor(0,0);
lcd.print("START SAVE RESULTS");
delay(1000);
readdata = 2;
}
}
void SaveResults()
{
pinMode(SS, OUTPUT);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Initializing SD Card");
if (!SD.begin(chipSelect))
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Initializing SD Card");
lcd.setCursor(5,1);
lcd.print("= failed!");
return;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Initializing SD Card");
lcd.setCursor(5,1);
lcd.print("= DONE!");
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile)
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("SAVED");
//---------------------------------------------
myFile.println("Results for weight ");
myFile.print(n);
myFile.print(" Kilogram");
myFile.println( );
myFile.println("Time = ");
myFile.print(ttm);
myFile.print(" Minute");
myFile.println("Distance = ");
myFile.print(ccnt);
myFile.print(" Metre");
myFile.println("Calories Burn = ");
myFile.print(Call);
myFile.print(" KCal");
myFile.println("SAVE COMPLETE");
myFile.close();
//---------------------------------------------
}
else
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("error opening file!");
}
readdata = 3;
}
void Finished()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DONE");
lcd.setCursor(0,1);
lcd.print("PLEASE PRESS");
lcd.setCursor(0,2);
lcd.print("RESET BUTTON");
}
void loop()
{
while (readdata == 1)
{
Setdata();
}
while (readdata == 0)
{
BTandCal();
}
while (readdata == 2)
{
SaveResults();
}
while (readdata == 3)
{
Finished();
}
}
I want to save data (ccnt,ttm,call,) to my SD card and this is my code .
What i wrong about saving data ?