Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 6 months ago.
Sd card library is creating problem in program. Cant see anything on LCD if add sdfile system code.
-
#include "mbed.h"
#include "TextLCD.h"
#include "SDFileSystem.h"
FileSystem sd(D9, D10, D12, D13, "sd");
TextLCD lcd(A0, A1, A2, A3, A4, A5);
DigitalOut switchengine(LED2);
DigitalIn pin_up(D6);
DigitalIn pin_down(D7);
InterruptIn pin_start(D8);
DigitalIn pin_delay(D11);
InterruptIn Pin_rain(PC_13);
Timer timer;
int Quarters;
void displayUpdate();
void startRun() ;
void callstart();
void timerf();
void pumpoff();
bool status ;
float delay;
int i;
int d;
int s;
int m;
float reverse;
float h;
float time;
int begin;
void rainsense();
time_t seconds = time(NULL);
//called when 15min has passed
void pumpoff(){
switchengine=0;
status = true;
//update display
lcd.cls();
lcd.locate(3,0);
lcd.printf("PUMP OFF!");
lcd.locate(0,1);
lcd.printf("Ran T:%1.2f Hr",i*0.25);
pin_start.rise(&timerf);
pin_start.fall(&callstart);
Pin_rain.rise(&rainsense);
}
void rainsense(){
switchengine=0;
lcd.cls();
lcd.locate(0,0);
lcd.printf("%s",ctime(&seconds));
if(h<1.1){
lcd.locate(0,1);
lcd.printf("Ran T:%1.2f Hr ",m);
}
else{
lcd.locate(0,1);
lcd.printf("Ran T:%1.2f Hr ",h);}
FILE *file = fopen("/local/rainstamp.txt", "a");
if(file == NULL) {
fprintf(file, "Could not open file for write\r\n");
}
else{
fprintf(file, " \r set time- rantime-%1.2f,Rain time- %s,\n", h , ctime(&seconds));
fclose(file);
}
while(1){
switchengine=0;
}
}
void timerf(){
timer.start();
begin = timer.read();
}
void callstart(){
int end = timer.read();
timer.stop();
int t = end - begin;
if(status==true ){
if(t==2){
startRun();
timer.reset();
}
}
}
void displayUpdate(){
//engine should be running
if (i==0){
pumpoff();
}
else{
switchengine=1;
if(i<4){
m = i*2;
s = 60;
while(m > 0){
lcd.cls();
lcd.locate(3,0);
lcd.printf("Time= %d min ",i*2);
lcd.locate(2,1);
lcd.printf(" %d min %d",m-1, s);
wait(1);
s--;
if(s==0){
m--;
s = 60;}
if(m==0){
pumpoff();}
Pin_rain.rise(&rainsense);
}
}
else{
s = 60;
h = i*0.25;
while(h > 0){
if(h<1.1){
lcd.cls();
lcd.locate(1,0);
lcd.printf("Time= %1.2f Hr ",i*0.25);
lcd.locate(2,1);
lcd.printf(" %1.1f min %d",(h-0.01)*100, s);
}
else{
lcd.cls();
lcd.locate(1,0);
lcd.printf("Time =%1.2f Hr ",i*0.25);
lcd.locate(2,1);
lcd.printf(" %1.2f Hr %d",h-0.01, s);
}
wait(1);
s--;
if(s==0){
h= h-0.01;
reverse = reverse+0.01;
s = 60;}
if(h==0){
pumpoff();}
Pin_rain.rise(&rainsense);
}
}
}
}
//update the display and start/stop the engine
// init and start the run
void startRun() {
status = false;
lcd.cls();
lcd.locate(3,0);
lcd.printf("PUMP START");
wait(1);
lcd.cls();
displayUpdate();
}
int main()
{
pin_up.mode(PullDown);
pin_down.mode(PullDown);
pin_start.mode(PullDown);
pin_delay.mode(PullDown);
//mkdir("/sd/mydir", 0777);
set_time(1458986114);
if(reverse < (i*0.25)){
lcd.locate(0,1);
lcd.printf("Ran T:%1.2f Hr",reverse);
}
if(switchengine==0){
lcd.cls();
lcd.locate(5,0);
lcd.printf("READY");
wait(1);
}
lcd.cls();
lcd.locate(3,0);
lcd.printf("SET TIME");
do{
while(pin_delay == 1){
d++;
lcd.cls();
lcd.locate(0,0);
lcd.printf("DELAY = %d mn", d);
wait(0.8);
}
if(pin_up==1)
{i= i++;
wait(0.1);
if (i>200)
i=200;
tim = i;
if(i<4){
lcd.cls();
lcd.locate(1,0);
lcd.printf("Time= %1.2f mn", tim*15);
}
else{
lcd.cls();
lcd.locate(1,0);
lcd.printf("Time= %1.2f Hr", tim*15/60);
}
}
else if(pin_down==1){
i=i--;
wait(0.1);
if( i<0)
i=0;
tim = i;
if(i<4){
lcd.cls();
lcd.locate(1,0);
lcd.printf("Time= %1.2f mn", tim*15);
}
else{
lcd.cls();
lcd.locate(1,0);
lcd.printf("Time= %1.2f Hr",tim*15/60);
}
}
} while(pin_start==0);
if(i==0){
while(1){
lcd.cls();
lcd.locate(3,0);
lcd.printf("Error!!!");
lcd.locate(1,1);
lcd.printf("Time not set");
wait(3);
}
}
tim = Quarters; //Initial selected number of quarters
if(d==0)
{
startRun() ;
}
else
{
int sec = 60;
int min = d*15;
if(d<4){
while(min>0){
lcd.cls();
lcd.locate(1,0);
lcd.printf("Wait =%d min %d",min-1, sec);
wait(1);
sec--;
if(sec==0){
min--;
sec = 60;}
if(min==0){
d=0;}
Pin_rain.rise(&rainsense);
}
}
else{
int sec = 60;
float hr = d*0.25;
while(hr>0){
lcd.cls();
lcd.locate(0,0);
lcd.printf("Wait= %1.2f Hr %d",hr-0.01, sec);
wait(1);
sec--;
if(sec==0){
hr= hr-0.01;
sec = 60;}
if(hr==0){
d = 0;}
Pin_rain.rise(&rainsense);
}
}
startRun();
}
// call to (re)start engine run)&& status == true if(runtimeleft == 0 )
while(1); // just wait for keypress
}
Question relating to:
Assigned to 9 years, 5 months ago.
This means that the question has been accepted and is being worked on.
1) Use the format option in the compiler.
2)
FILE *file = fopen("/local/rainstamp.txt", "a"); if(file == NULL) { fprintf(file, "Could not open file for write\r\n"); }????????? You check if the file pointer is invalid and then try to use it anyway? If you are getting a null pointer that's going to cause a crash.
posted by Andy A 08 Jun 2016Do you mean to say if I am running the program in the controller without SDcard breakout board, just for test, the whole program wont work? because I was running program just to test without connecting any sdcard.
posted by jayendra mishra 08 Jun 2016