7 years, 4 months ago.  This question has been closed. Reason: Off Topic

.

.

1 Answer

7 years, 4 months ago.

Hello Minsuk,

Try the code below. Never been tested with real hardware but may work.

#include "mbed.h"
#include "uLCD_4DGL.h"
#include "ultrasonic.h"

// RGB LED Setup
class   RGBLed
{
public:
    RGBLed(PinName redpin, PinName greenpin, PinName bluepin);
    void    write(float red, float green, float blue);
private:
    PwmOut  _redpin;
    PwmOut  _greenpin;
    PwmOut  _bluepin;
};

RGBLed::RGBLed(PinName redpin, PinName greenpin, PinName bluepin) :
    _redpin(redpin),
    _greenpin(greenpin),
    _bluepin(bluepin) {
    //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker)
    _redpin.period(0.0005);
}

void RGBLed::write(float red, float green, float blue) {
    _redpin = red;
    _greenpin = green;
    _bluepin = blue;
}

volatile bool   distanceChanged = false;
volatile int    newDistance;
volatile bool   updateTemperature = true;
DigitalIn       alarm(p29, PullUp);         //internal pull up
DigitalOut      led1(LED1);
DigitalOut      led2(LED2);
Timeout         distanceTimeout;
Timeout         temperatureTimeout;
Timeout         alarmTimeout;
float           tempC, tempF;
AnalogIn        TMP36(p15);
uLCD_4DGL       uLCD(p9, p10, p11);         // serial tx, serial rx, reset pin;
RGBLed          myRGBled(p23, p22, p21);    //RGB PWM pins

void  turnLed1Off(void);
void  turnLed2Off(void);
void  dist(int distance);
void  nextUpdate(void);

ultrasonic      mu(p6, p7, .1, 1, &dist);   //Set the trigger pin to p6 and the echo pin to p7

void turnLed1Off(void) {
    led1 = 0;
}

void turnLed2Off(void) {
    led2 = 0;
}

void dist(int distance) {
    distanceChanged = true;
    newDistance = distance;
    if ((distance < 3200) && (led2 != 1)) {
        led2 = 1;
        distanceTimeout.attach(callback(&turnLed2Off), 2.0);
    }
}

void nextUpdate(void) {
    updateTemperature = true;
}

int main(void) {
    mu.startUpdates();
    while (1) {
        if (updateTemperature) {
            tempC = ((TMP36 * 3.3) - 0.600) * 100.0;
            tempF = (9.0 * tempC) / 5.0 + 32.0;
            uLCD.locate(0, 0);
            uLCD.printf("%5.2F%cF", tempF, 176);
            if (tempF > 80) {
                uLCD.locate(0, 1);
                uLCD.printf("HOT ");
                myRGBled.write(1.0, 0.0, 0.0);  //red
            }
            else
            if (tempF < 66) {
                uLCD.locate(0, 1);
                uLCD.printf("COLD");
                myRGBled.write(0.0, 0.0, 1.0);  //blue
            }
            else {
                uLCD.locate(0, 1);
                uLCD.printf("FAIR");
                myRGBled.write(0.0, 1.0, 0.0);  //green
            }
            updateTemperature = false;
            temperatureTimeout.attach(callback(&nextUpdate), 1.0);   // next update after 1s
        }
        mu.checkDistance();
        if (distanceChanged) {
            uLCD.locate(0, 2);
            uLCD.printf("%dmm", newDistance);
            distanceChanged = false;
        }
        if ((!alarm) && (led1 != 1)) {
            led1 = 1;
            alarmTimeout.attach(callback(&turnLed1Off), 2.0);       //  after 2s turn LED1 off
        }
    }
}

Accepted Answer

Hello!

Thank you for the quick help! The temperature does constantly refresh, however the PIR and the ultrasound is not working. The ultrasound holds the same value for the distance...

posted by Minsuk Chun 08 Jul 2017

Sounds promising. So let's make the ultrasound and LED2 work. Double check the wiring. Then use the code below and try to move the object so that the distance becomes less than 3200mm. Let me know how it works.

#include "mbed.h"
#include "uLCD_4DGL.h"
#include "ultrasonic.h"

volatile bool   distanceChanged = false;
volatile int    newDistance;
DigitalOut      led2(LED2);
Timeout         distanceTimeout;
uLCD_4DGL       uLCD(p9, p10, p11);     // serial tx, serial rx, reset pin;

void  turnLed2Off(void);
void  dist(int distance);

ultrasonic mu(p6, p7, .1, 1, &dist);   //Set the trigger pin to p6 and the echo pin to p7

void turnLed2Off(void) {
    led2 = 0;
}

void dist(int distance) {
    distanceChanged = true;
    newDistance = distance;
    if ((distance < 3200) && (led2 != 1)) {
        led2 = 1;
        distanceTimeout.attach(callback(&turnLed2Off), 2.0);
    }
}

int main(void) {
    mu.startUpdates();
    while (1) {
        mu.checkDistance();
        if (distanceChanged) {
            uLCD.locate(0, 0);
            uLCD.printf("%dmm", newDistance);
            distanceChanged = false;
        }
    }
}
posted by Zoltan Hudak 09 Jul 2017

This code works for the Ultrasound!

posted by Minsuk Chun 09 Jul 2017

Let's add temperature.

#include "mbed.h"
#include "uLCD_4DGL.h"
#include "ultrasonic.h"

volatile bool   distanceChanged = false;
volatile int    newDistance;
volatile bool   updateTemperature = true;
DigitalOut      led2(LED2);
Timeout         distanceTimeout;
Timeout         temperatureTimeout;
float           tempC, tempF;
AnalogIn        TMP36(p15);
uLCD_4DGL       uLCD(p9, p10, p11);         // serial tx, serial rx, reset pin;

void  turnLed2Off(void);
void  dist(int distance);

ultrasonic      mu(p6, p7, .1, 1, &dist);   //Set the trigger pin to p6 and the echo pin to p7

void turnLed2Off(void) {
    led2 = 0;
}

void dist(int distance) {
    distanceChanged = true;
    newDistance = distance;
    if ((distance < 3200) && (led2 != 1)) {
        led2 = 1;
        distanceTimeout.attach(callback(&turnLed2Off), 2.0);
    }
}

void nextUpdate(void) {
    updateTemperature = true;
}

int main(void) {
    mu.startUpdates();
    while (1) {
        if (updateTemperature) {
            tempC = ((TMP36 * 3.3) - 0.600) * 100.0;
            tempF = (9.0 * tempC) / 5.0 + 32.0;
            uLCD.locate(0, 0);
            uLCD.printf("%5.2F%cF", tempF, 176);
            updateTemperature = false;
            temperatureTimeout.attach(callback(&nextUpdate), 1.0);   // next update after 1s
        }
        mu.checkDistance();
        if (distanceChanged) {
            uLCD.locate(0, 1);
            uLCD.printf("%dmm", newDistance);
            distanceChanged = false;
        }
    }
}
posted by Zoltan Hudak 10 Jul 2017

That also works! :D Still confused about the method for PIR though.... :(

posted by Minsuk Chun 10 Jul 2017

Keep in mind that mbed works at 3.3V. So you have to make also the SEN-08630 PIR to work at 3.3V. According to the description available at https://www.sparkfun.com/products/13285 this can be achieved by installing a jumper wire past the 5V regulator on the PIR board. Once done try the code I have posted in my first answer. Notice that it contains some modifications. The reason I put it there is not to exceed the 3000 char limitation for this page.
NOTE: In addition to configuring mbed's DigitalInput with pull up resistor you can install also an external 4k7 resistor between the PIR's ouput line and +3.3V. Also remember to connect the PIRs ground to the mbed's ground.

posted by Zoltan Hudak 14 Jul 2017