Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Intelligent Lamp with makeblock inventor electronic kit. 2 versions uncommenting some lines
/************************************************************************
* File Name : Smart_table_lamp.ino
* Author : crazyfeng
* Version : V1.0
* Date : 18/07/2014
*Parts required : Smart_table_lamp
* Description :The lamp can light up when someone make sound or go through.
* The Brightness of the lamp is adjustable.
* License : CC-BY-SA 3.0
* Copyright (C) 2013 Maker Works Technology Co., Ltd. All right reserved.
* http://www.makeblock.cc/
**************************************************************************/
#include <Makeblock.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
MeLightSensor myLight(PORT_5);
MeSoundSensor mySound(PORT_6);
MePotentiometer myPotentiometer(PORT_7);
MePIRMotionSensor myPIRsensor(PORT_8);
MeRGBLed led(PORT_3);
unsigned int bri0, bri1;
int8_t TimeDisp[] = {0x00,0x00,0x00,0x00};
unsigned char second;
unsigned char minute = 0;
unsigned char hour = 12;
long lastTime = 0;
int lvalue = 0;
Me7SegmentDisplay disp(PORT_4);
void setup()
{
Serial.begin(9600);
led.setNumber(4);
disp.set();
disp.init();
Serial.print("setup done!");
}
void loop()
{
Serial.println(myPotentiometer.read());
if (myPotentiometer.read() <= 10) {
Sound_contral();
}
if (myPotentiometer.read() > 975) {
PIRsensor_contral();
//Light_contral();
}
while(myPotentiometer.read() > 10 && myPotentiometer.read() < 975)
{
Serial.println(myPotentiometer.read());
Potentiometer_contral();
if(millis()-lastTime>=1000){
TimingISR();
TimeUpdate();
disp.display(TimeDisp);
lastTime = millis();
}
}
}
void Sound_contral()
{
unsigned int voice = mySound.strength();
Serial.print("voice:");
Serial.println(voice);
if(voice < 400 || voice > 500)
{
Serial.println("B");
for (uint8_t t = 0; t < 4; t++)
{
led.setColorAt(t, 0, 0, 0);
}
led.show();
}
else
{
Serial.println("A");
for (uint8_t t = 0; t < 4; t++)
{
led.setColorAt(t, 150, 150, 150);
}
led.show();
delay(100);
}
}
void PIRsensor_contral()
{
if(myPIRsensor.isPeopleDetected())
{
Serial.print("People Detected!");
for (uint8_t t = 0; t < 4; t++)
{
led.setColorAt(t, 150, 150, 150);
}
led.show();
delay(5000);
}
else
{
for (uint8_t t = 0; t < 4; t++)
{
led.setColorAt(t, 0, 0, 0);
}
led.show();
}
}
void Light_contral()
{
lvalue = myLight.read();
Serial.print("llum:");
Serial.println(lvalue);
if (lvalue < 100) {
for (uint8_t t = 0; t < 4; t++)
{
led.setColorAt(t, 150, 150, 150);
}
led.show();
} else {
for (uint8_t t = 0; t < 4; t++)
{
led.setColorAt(t, 0, 0, 0);
}
led.show();
}
}
void Potentiometer_contral()
{
bri0 = myPotentiometer.read();
if(bri0 > 20)
{
for (uint8_t t = 0; t < 4; t++)
{
bri1 = bri0 / 4;
led.setColorAt(t, bri1, bri1, bri1);
}
led.show();
}
else
{
for (uint8_t t = 0; t < 4; t++)
{
led.setColorAt(t, 0, 0, 0);
}
led.show();
}
}
void TimingISR()
{
second ++;
if(second == 60)
{
minute ++;
if(minute == 60)
{
hour ++;
if(hour == 24)hour = 0;
minute = 0;
}
second = 0;
}
}
void TimeUpdate(void)
{
TimeDisp[0] = minute / 10;
TimeDisp[1] = minute % 10;
TimeDisp[2] = second / 10;
TimeDisp[3] = second % 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment