SMART BLINDS 12 – Stepper Motor Integration & Prototype

Setup: Same as last 2 blogs but with motor driver signal pins plugged into logic board (left):

fritzing.PNG

This is our current wiring. Unfortunately we ran into a few problems with our arduino running out of memory. The Arduino on the left is responsible for all the logic, this takes up a substantial amount of code, over 430 lines!! The meant that we hit over 93% memory usage before even integrating the code for the stepper motor. As seen here:memoryerror

our next stage is to try and reduce memory usage so we can add in the stepper motor to our logic board. Even the Simplest motor code takes over 10% of memory to function. We estimate around a further 15% will need to be freed up for our use case.

User interface improvements:

While we were working on this i also changed the code so that when the user changes wither the mode or user temp then it would output this to the LCD. Then when the next set of temps comes round it defaults back to displaying the In and Out temperature readings. This means that after initial set-up the arduino can run quite happily by itself, independent of any PC.

usermodeuser temp

Working prototype:

This is our final Working prototype! We integrated our existing code used in the stepper motor blog before and ran into a few problems, however after some tinkering and adding of some new code:Newcode

We were able to get the motor runing when it was suppose to! as seen here:

The next stage was to mount the motor to our ‘window frame’  and test to see if it could pull the blind. we used a 2nd hand blind found for free on the internet.

Final Model.jpg

Below is the mounting for the motion sensor and the LDR. Thermo sensors were mounted at the top of the frame as shown in picture above.

motion.jpg

Final Code:


 

// First we include the libraries
#include 
#include 
#include 
#include 

#define ONE_WIRE_BUS A5

const int stepsPerRevolution = 360; // Number of steps
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Assign the addresses of the 1-Wire temp sensors.
DeviceAddress TempIn = { 0x28, 0xFF, 0x3A, 0xAB, 0xC2, 0x17, 0x04, 0xB3 };
DeviceAddress TempOut = { 0x28, 0xFF, 0x16, 0xA5, 0xC3, 0x17, 0x05, 0x8A };

LiquidCrystal lcd(13, 12, 7, 6, 5, 4);

const int Interval = 10000;
unsigned long previousMillis = 0;

 

#define LDR A0
#define PIR A1
#define LeftSwitch 2
#define RightSwitch 3

int LDRRead = 0;
int PIRRead = 0;

int userTemp = 0;
float InsideTemp = 0;
float OutsideTemp = 0;

bool BlindOpen = true;
int BlindState = 0;
int Set_Mode = 2;
bool SetUpComplete = false;

bool UserInRoom = false;
bool Night = false;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Smart Blind Version 0.420");
// Start up the library
sensors.begin();
sensors.setResolution(TempIn, 10);
sensors.setResolution(TempOut, 10);
lcd.begin(16, 2);
lcd.setCursor (0, 0);
lcd.print("SmartBlind v4.20");
lcd.setCursor (0, 1);
lcd.print("Initializing...");
pinMode(LDR, INPUT);
pinMode(PIR, INPUT);
delay(1500);
pinMode(LeftSwitch, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(LeftSwitch), LeftDigit, FALLING);
pinMode(RightSwitch, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RightSwitch), RightDigit, FALLING);

myStepper.setSpeed(42); // set the speed at 60 rpm:

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop(void) {

if (SetUpComplete == false) {

ModeChoice();
Serial.println();
SetTemp();
Serial.println();
SetUpComplete = true;
}
else {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > Interval) {
previousMillis = currentMillis;

 

if (Set_Mode == 1) {
Serial.println();
Serial.println("----------------------Start Of Sensing----------------------");
//PIR_Sense();
//NightOrDay();
CallTemp();
AssignTemp();
LcdPrintTemp();
SavingsMode();
ForwardOrBackwards();
}
if (Set_Mode == 2) {
Serial.println();
Serial.println("Smart Mode");
PIR_Sense();
NightOrDay();
CallTemp();
AssignTemp();
LcdPrintTemp();
SmartMode();
ForwardOrBackwards();
}
if (Set_Mode == 3) {
SetUpComplete = false;
}

}
else {
Serial.print("");
}

}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void ForwardOrBackwards (){
Serial.println(BlindOpen);
Serial.println(BlindState);
if ((BlindOpen == true) && BlindState == 1) {}
if ((BlindOpen == true) && BlindState == 0) {
myStepper.step(stepsPerRevolution);
BlindState = 1;
}
if ((BlindOpen == false) && BlindState == 1) {
myStepper.step(-stepsPerRevolution);
BlindState = 02
231;
}
if ((BlindOpen == false) && BlindState == 0) {}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void LeftDigit ()
{ if (SetUpComplete == false) {}
if (SetUpComplete == true) {
Set_Mode = Set_Mode + 1;
Serial.print(Set_Mode);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("User Mode: ");
lcd.print(Set_Mode);
if (Set_Mode > 1 ) {
Set_Mode = 0;
}
else {}
}
}

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void RightDigit ()
{
if (SetUpComplete == false) {
}
if (SetUpComplete == true) {
userTemp = userTemp + 1;
Serial.println(userTemp);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("User Temp: ");
lcd.print(userTemp);
if (userTemp > 38) {
userTemp = 10;
}
else {}
}
}

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void SmartMode() {

//----------------------------------------Scenario 1: Warming----------------------------------------

if ((((InsideTemp < userTemp) && InsideTemp < OutsideTemp) && UserInRoom == false) && Night == false)
{
Serial.print("Day: Inside is Too Cold, Outside is Warmer - Opening blinds to let in heat");
Serial.println();
BlindOpen = true;
// BlindState = 1;
}
if ((((InsideTemp < userTemp) && InsideTemp < OutsideTemp) && UserInRoom == true) && Night == false)
{
Serial.print("User in room during Day: opening blinds for light");
Serial.println();
BlindOpen = true;
// BlindState = 1;
}
if ((((InsideTemp < userTemp) && InsideTemp < OutsideTemp) && UserInRoom == false) && Night == true)
{
Serial.print("Night: Inside is Too Cold, Outside is Warmer - Opening blinds to let in heat");
Serial.println();
BlindOpen = true;
// BlindState = 1;
}
if ((((InsideTemp < userTemp) && InsideTemp  userTemp) && InsideTemp  userTemp) && InsideTemp  userTemp) && InsideTemp  userTemp) && InsideTemp < OutsideTemp) && UserInRoom == true) && Night == true)
{
Serial.print("Night: User in room: Closing blinds for privacy");
Serial.println();
BlindOpen = false;
// BlindState = 0;
}

//----------------------------------------Scenario 3: Too Cold----------------------------------------

if ((((InsideTemp  OutsideTemp) && UserInRoom == false) && Night == false)
{
Serial.print("Day: Inside is Too Cold, Outside is Cooler: Closing blinds to save heat");
Serial.println();
BlindOpen = false;
// BlindState = 0;
}
if ((((InsideTemp  OutsideTemp) && UserInRoom == true) && Night == false)
{
Serial.print("Day: User in room during Day: opening blinds for light");
Serial.println();
BlindOpen = true;
// BlindState = 1;
}
if ((((InsideTemp  OutsideTemp) && UserInRoom == false) && Night == true)
{
Serial.print("Night: Inside is Too Cold, Outside is Cooler: Closing blinds to save heat");
Serial.println();
BlindOpen = false;
// BlindState = 0;
}
if ((((InsideTemp  OutsideTemp) && UserInRoom == true) && Night == true)
{
Serial.print("Night: User in room: Closing blinds for privacy");
Serial.println();
BlindOpen = false;
// BlindState = 0;
}

//----------------------------------------Scenario 4: Cooling----------------------------------------

if ((((InsideTemp > userTemp) && InsideTemp > OutsideTemp) && UserInRoom == false) && Night == false)
{
Serial.print("Day: Inside is Too Hot, Outside is Cooler: Opening blinds to let out heat");
Serial.println();
BlindOpen = true;
// BlindState = 1;
}
if ((((InsideTemp > userTemp) && InsideTemp > OutsideTemp) && UserInRoom == true) && Night == false)
{
Serial.print("Day: User in room during Day: opening blinds for light");
Serial.println();
BlindOpen = true;
// BlindState = 1;
}
if ((((InsideTemp > userTemp) && InsideTemp > OutsideTemp) && UserInRoom == false) && Night == true)
{
Serial.print("Night: Inside is Too Hot, Outside is Cooler: Opening blinds to let out heat");
Serial.println();
BlindOpen = true;
// BlindState = 1;
}
if ((((InsideTemp > userTemp) && InsideTemp > OutsideTemp) && UserInRoom == false) && Night == true)
{
Serial.print("Night: User in room: Closing blinds for privacy");
Serial.println();
BlindOpen = false;
// BlindState = 0;
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void SavingsMode() {
if ((InsideTemp < userTemp) && InsideTemp  userTemp) && InsideTemp < OutsideTemp)
{
Serial.print("*Inside is Too Hot, Outside is Warmer: Closing blinds to reduce heat transfer*");
Serial.println();
BlindOpen = false;
// BlindState = 0;
}
if ((InsideTemp  OutsideTemp)
{
Serial.print("*Inside is Too Cold, Outside is Cooler: Closing blinds to save heat*");
Serial.println();
BlindOpen = false;
// BlindState = 0;
}
if ((InsideTemp > userTemp) && InsideTemp > OutsideTemp)
{
Serial.print("*Inside is Too Hot, Outside is Cooler: Opening blinds to let out heat*");
Serial.println();
BlindOpen = true;
// BlindState = 1;
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void ModeChoice () {
if (SetUpComplete == false) {
Serial.println();
Serial.print("Do you want 'Smart' mode [2] Or 'Maximum Savings' mode? [1]... ");

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Smart / Savings?");

while (Serial.available() == 0) {}
if (Serial.available() > 0) {
Set_Mode = Serial.parseInt();
Serial.print(Set_Mode);
lcd.clear();
}
}
// else {
// if (Set_Mode == 2) {
// Serial.println();
// Serial.println("Manual Mode Enabled:");
// lcd.clear();
// }
if (Set_Mode == 2) {
Serial.println();
Serial.println("Smart Mode Enabled:");
lcd.clear();
}
if (Set_Mode == 1) {
Serial.println();
Serial.println("Savings Mode Enabled:");
lcd.clear();
}
}

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void PIR_Sense () {
PIRRead = digitalRead(PIR);
if (PIRRead == HIGH) {

Serial.println("----------------------Start Of Sensing----------------------");
Serial.println("Motion detected!");
UserInRoom = true;

}
else {
Serial.println("----------------------Start Of Sensing----------------------");
Serial.println("Motion stopped!");
UserInRoom = false;
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void NightOrDay () {
LDRRead = analogRead(LDR);
if (LDRRead  0) {
userTemp = Serial.parseInt();
Serial.print(userTemp);
lcd.clear();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void printTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

if (tempC == -127.00)
{
Serial.print("Error getting temperature ");
}
else
{
Serial.print(tempC);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CallTemp() {
Serial.println();
Serial.println("Getting temperatures... ");
Serial.println();
sensors.requestTemperatures();
Serial.print("Inside Temp in Degrees C: ");
Serial.println();
printTemperature(TempIn);
Serial.println();
Serial.print("Outside Temp in Degrees C: ");
Serial.println();
printTemperature(TempOut);
Serial.println();

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s