Eddie RookeKelvin LimantoAndrew Nguyen
Published © GPL3+

MEGR 3171 IOT Project: Interactive Lamp

An Interactive Lamp that can be remotely controlled by sound, motion, and touch.

BeginnerFull instructions provided4 hours8
MEGR 3171 IOT Project: Interactive Lamp

Things used in this project

Hardware components

Argon
Particle Argon
×3
Breadboard (generic)
Breadboard (generic)
×3
TTP223B Capacitive Touch Sensor
Model: TTP223B Capacitive Touch Sensor Brand: HiLetgo
×2
Relay Module Switch
Model: HiLetgo 2pcs 5V One Channel Relay Module Relay Switch Brand: HiLetgo
×1
LED (generic)
LED (generic)
×2
Resistor 100k ohm
Resistor 100k ohm
×2
HC-SR501 Pir Motion IR Sensor
Model: HC-SR501 Brand: DIYmall
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Multitool, Screwdriver
Multitool, Screwdriver
Scissor, Electrician
Scissor, Electrician
Tape, Electrical
Tape, Electrical

Story

Read more

Schematics

T16MOSO Circuit Diagram

T16TOUCH Circuit Diagram

T16LAMP Circuit Diagram

Live Data Graph Capture

Communication Flow Chart

Code

T16LAMP Code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

int lamp = D7;
int lampState = LOW;

//toggle between auto and manual mode
//auto mode (HIGH) will use the pir sensor
//manual mode (LOW) will touch and sound sensor
int modeState = LOW;

TCPClient client;

unsigned long myChannelNumber =  1580958;
const char * myWriteAPIKey = "FQE3WRD0IAK9LAG1";

void setup() {
    pinMode(lamp, OUTPUT);
    digitalWrite(lamp, LOW);
    
    //subscribes this argon to the touch toggle event
    Particle.subscribe("touchToggle", touchToggleLamp, MY_DEVICES);
    
    //subscribes this argon to the sound toggle event
    Particle.subscribe("soundToggle", soundToggleLamp, MY_DEVICES);
    
    //subscribes this argon to the on and off events
    Particle.subscribe("lampOnOff", pirLampOnOff, MY_DEVICES);
    
    //subscibes this argon to the mode toggle event
    Particle.subscribe("modeToggle", modeToggleState, MY_DEVICES);
    
    ThingSpeak.begin(client);
}


//toggles the light on and off from the touch sensor
void touchToggleLamp(const char *event, const char *data) {
    
    if (modeState == LOW) {
        //toggles the lamp state if the touch sensor is triggered
        lampState = !lampState;
        
        //sends lamp state change event to other argons
        Particle.publish("lampChange", PRIVATE);
    }
}

//toggles the light on and off from the sound sensor
void soundToggleLamp(const char *event, const char *data) {
    
    if (modeState == LOW) {
        //toggles the lamp state if the touch sensor is triggered
        lampState = !lampState;
        
        //sends lamp state change event to other argons
        Particle.publish("lampChange", PRIVATE);
    }
}

//turns the light on and off from the motion sensor
void pirLampOnOff (const char *event, const char *data) {
    if (strcmp(data, "off") == 0) {
        lampState = LOW;
    }
    
    else if (strcmp(data, "on") == 0) {
        lampState = HIGH;
    }
    Particle.publish("lampChange", PRIVATE);
}

//toggles between auto and manual sensor modes
void modeToggleState(const char *event, const char *data) {
    if (strcmp(data, "manual") == 0) {
        modeState = LOW;
    }
    
    else if (strcmp(data, "auto") == 0) {
        modeState = HIGH;
    }
}

void loop() {
    
    ThingSpeak.setField(1, lampState);
    Serial.print(lampState);
    Serial.println("Lamp State");
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    
    //updates the lamp state to the current lamp state
    digitalWrite(lamp, lampState);
}

T16TOUCH Code

C/C++
Type processes here
int led = D7;
int button = D6;
int modeButton = D5;
int modeLed = D4;
int sound = D3;
int soundLed = D2;
int lastButtonState;
int currentButtonState;
int modeLastButtonState;
int modeCurrentButtonState;
int soundCurrentState;
int soundLastState;
int lampState = LOW;
int modeState = LOW;

void setup() {
     
    pinMode(led, OUTPUT);
    digitalWrite(led, LOW);
    currentButtonState = digitalRead(button);
    pinMode(button, INPUT);
    pinMode(modeLed, OUTPUT);
    digitalWrite(modeLed, LOW);
    modeCurrentButtonState = digitalRead(modeButton);
    pinMode(modeButton, INPUT);
    pinMode(sound, INPUT);
    soundCurrentState = digitalRead(sound);
    
    //subscribes this argon to the lamp change event
    Particle.subscribe("lampChange", blinkLight, MY_DEVICES);
}

//blinks the D7 led 3 times, if the lamp changes state
void blinkLight(const char *event, const char *data) {
    digitalWrite(led, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, lampState);
}

void blinkSoundLed(){
    digitalWrite(soundLed, HIGH);
    delay(100);
    digitalWrite(soundLed, LOW);
    delay(100);
    digitalWrite(soundLed, HIGH);
    delay(100);
    digitalWrite(soundLed, LOW);
    delay(100);
    digitalWrite(soundLed, HIGH);
    delay(100);
    digitalWrite(soundLed, LOW);
    delay(100);
    digitalWrite(soundLed, HIGH);
    delay(100);
    digitalWrite(soundLed, LOW);
}

void loop() {
    lastButtonState = currentButtonState;
    currentButtonState = digitalRead(button);
    
    //if statement is excecuted if touch sensor is triggered
    if (lastButtonState == HIGH && currentButtonState == LOW) {
        lampState = !lampState;
        digitalWrite(led, lampState);
        
        //tells the lamp argon to toggle on or off
        Particle.publish("touchToggle", PRIVATE);
    }
    
    modeLastButtonState = modeCurrentButtonState;
    modeCurrentButtonState = digitalRead(modeButton);
    
    //if statement is excecuted if mode touch sensor is triggered
    if (modeLastButtonState == HIGH && modeCurrentButtonState == LOW) {
        modeState = !modeState;
        digitalWrite(modeLed, modeState);
        
        //updates the mode state on the lamp argon
        if(modeState == LOW){
            Particle.publish("modeToggle", "manual", PRIVATE);
        }
        else {
            Particle.publish("modeToggle", "auto", PRIVATE);
        }
    }
    
    soundLastState = soundCurrentState;
    soundCurrentState = digitalRead(sound);
    
    if (soundLastState == HIGH && soundCurrentState == LOW) {
        lampState = !lampState;
        digitalWrite(led, lampState);
        
        //blinks the sound led if the sound sensor hears a loud noise
        blinkSoundLed();
        
        //tells the lamp argon to toggle on or off
        Particle.publish("soundToggle", PRIVATE);
    }
}

T16MOSO Code

C/C++
TYpe processes here
int pirIn = D4;
int led = D7;
int statusLed = D6;
int state = LOW;
int val = 0;
int calibrationTime = 15000;
int modeState = LOW;
int lampState = LOW;
int secondsCounter = 0;

//time in seconds for the light to automatically turn off
//if the motion sensor does not detect movement
int motionResetTime = 10;

void setup() {
    
    Particle.subscribe("modeToggle", modeToggleState, MY_DEVICES);
    Particle.subscribe("lampChange", blinkLight, MY_DEVICES);
    
    pinMode(pirIn, INPUT);
    pinMode(led, OUTPUT);
    digitalWrite(led, LOW);
    pinMode(statusLed, OUTPUT);
    digitalWrite(statusLed, LOW);
    delay(calibrationTime);
    digitalWrite(led, HIGH);
    digitalWrite(statusLed, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    digitalWrite(statusLed, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    digitalWrite(statusLed, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    digitalWrite(statusLed, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    digitalWrite(statusLed, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    digitalWrite(statusLed, LOW);
    
    //converts auto turn off time from seconds to miliseconds
    motionResetTime = motionResetTime * 1000;
}

//blinks the D7 led 3 times, if the lamp changes state
void blinkLight(const char *event, const char *data) {
    digitalWrite(led, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, lampState);
}


void modeToggleState(const char *event, const char *data) {
    if (strcmp(data, "manual") == 0) {
        modeState = LOW;
    }
    
    else if (strcmp(data, "auto") == 0) {
        modeState = HIGH;
    }
}

void readTheSensor() {
    val = digitalRead(pirIn);
    if (val == HIGH) {
        lampState = HIGH;
        secondsCounter = 0;
    }
    else {
        secondsCounter = secondsCounter + 50;
    }
}

void updateLed() {
    if (val == HIGH){
        state = HIGH;
    }
    else {
        state = LOW;
    }
    digitalWrite(led, state);
}

void updateLampOnOff() {
    if (modeState == HIGH ) {
        if (lampState == HIGH) {
            Particle.publish("lampOnOff", "on", PRIVATE);
            //Particle.publish("touchToggle", PRIVATE);
            digitalWrite(statusLed, lampState);
        }
        else {
            Particle.publish("lampOnOff", "off", PRIVATE);
            //Particle.publish("touchToggle", PRIVATE);
            digitalWrite(statusLed, lampState);        
        }
        delay(1000);
    }
}

void loop() {
        readTheSensor();
        updateLed();
        
        //set the wait time to automatically turn off in miliseconds
        if (secondsCounter%motionResetTime == 0) {
            updateLampOnOff();
            lampState = LOW;
        }
        delay(50);
}

Credits

Eddie Rooke

Eddie Rooke

0 projects • 0 followers
Kelvin Limanto

Kelvin Limanto

0 projects • 0 followers
Andrew Nguyen

Andrew Nguyen

0 projects • 0 followers

Comments

Add projectSign up / Login