8 years, 8 months ago.

how do i modify the code below to store data with respect to time, used for graph ploting.

  1. include "mbed.h"
  2. include "TextLCD.h"
  3. include "SDFileSystem.h"

SDFileSystem sd(p11, p12, p13, p9, "sd"); the pinout on the mbed Cool Components workshop board TextLCD lcd(p19, p20, p21, p22, p23, p24); rs, e, d4-d7 AnalogIn lightin(p18); defines Analog input for light at pin 18 float light; defines the light data as type float int main() { lcd.printf("light in lux!\n"); light = lightin; wait(0.5);

FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); if(fp == NULL) { error("Could not open file for write\n"); } else{ lcd.printf("SD card file successfully opened\n"); } fprintf(fp, "light:%2.1f", "time:%2.1f%%", light , time); fclose(fp);

}

Question relating to:

1 Answer

8 years, 8 months ago.

#include "mbed.h"
#include "TextLCD.h"
#include "AM2303.h"
TextLCD lcd(p19, p20, p21, p22,  p23, p24);                         
AM2303 h(p18);                                                                     
DigitalOut relay(p9);                                                              
int main()                                                                               
{
    int state;                                                                             
    /* Show splash screen */
    lcd.cls();
    lcd.locate(0, 0);
    lcd.printf("AM2303 Humidity");

    while(true) {                                                                         
        wait(2.0);                                                                        
        state = h.readData();                                                     
        lcd.locate(0, 1);                                                              /

        if (state != AM2303::OK) {
            lcd.printf("<Error: %d>", state);
        } else {
            lcd.printf("T:%2.1fC, H:%2.1f%%", h.readTemperature(), h.readHumidity());
        }

        if ((h.readTemperature() < 10) && ( h.readHumidity() < 30)  {          // If tem <10c & humity <30%
                    relay= 0;                               
        }  else {                                                
            relay = 1;                                     
        }                                                     

    }
}

Accepted Answer

It might be better to do it inside the if statement checking the state.

        if (state != AM2303::OK) {
            lcd.printf("<Error: %d>", state);
        } else {
            lcd.printf("T:%2.1fC, H:%2.1f%%", h.readTemperature(), h.readHumidity());
        
            if ((h.readTemperature() < 10) && ( h.readHumidity() < 30))  {          // If tem <10c & humity <30%
                relay= 0;                               
            }  else {                                                
                relay = 1;                                     
            }    
        }
posted by Stephen Paulger 05 Aug 2015

Good point.

It would also be a good idea to have a little smoothing and/or hysteresis in there so that the relay isn't thrashing around constantly when you are right on the transition point.

posted by Andy A 05 Aug 2015

Many thanks Stephen your idea was very helpful And Andy will be greatful if you can give my an idea how to go about.

posted by Prosper C 05 Aug 2015

How to add hysteresis? There are lots of different ways to structure it but is mainly comes down to moving the thresholds.

// default thresholds to turn the relay on.
float tempTheshold = 10;
float humidityThreshold = 30;

if (relay == 0) {                // if the previous result was below the threshold then raise the thresholds by 10%
  tempTheshold *= 1.1;
  humidityThreshold *= 1.1;
}

if ((h.readTemperature() < tempTheshold) && ( h.readHumidity() < humidityThreshold))  {    
  relay= 0;                               
}  else {                                                
  relay = 1;                                     
}

posted by Andy A 06 Aug 2015