Nathan Nguyen
Published

Lane Tech HS - PCL - Home Power Button!

Too lazy to walk and turn something on Want to cheaply control your home power usage while out Well here's a button (and app) to solve it!

BeginnerShowcase (no instructions)1 hour577
Lane Tech HS - PCL - Home Power Button!

Things used in this project

Hardware components

Photon
Particle Photon
×1
Breadboard (generic)
Breadboard (generic)
or a custom PCB to save space
×1
Jumper wires (generic)
Jumper wires (generic)
×1
RobotGeek Relay
RobotGeek Relay
or any power relay
×1
Adafruit Large Arcade Button with LED - 60mm Red
×1
Buzzer
Buzzer
×1

Software apps and online services

Blynk
Blynk

Story

Read more

Custom parts and enclosures

PCB + Photon

A close up of my PCB wiring

Arcade Button + Case

I used an old empty container to house my button

Project

Button in action

Code

Code!

C/C++
The start just initializes pins and imports Blynk's librarys.
Inside the loop is the core function of my project, toggling power.
Inside the rest of the loops are functions for Blynk, and notes for melody making (borrowed from ianklatzco)
// This #include statement was automatically added by the Particle IDE.
#define BLYNK_PRINT Serial
#include <blynk.h>
#include <SPI.h>

int pinButton = D6;
int pinLED = D4;
int pinRelay = D5;
//for tunes {
const int pinSpeaker = D3;
const int songLength = 3;
char notes[] = "azbC";
char beats[] = {2,3,3,16};
int tempo = 75/4;
//}
//for Blynk {
char auth[] = "your_photon";
//}

void setup() {
    
  pinMode(pinButton, INPUT_PULLUP);
  pinMode(pinLED, OUTPUT);
  pinMode(pinRelay, OUTPUT);
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);
  Blynk.begin(auth);

}

void loop() {
    Blynk.run();
    WidgetLED led1(V4);
    if(digitalRead(pinButton) == LOW) {
        delay(100);
        digitalWrite(pinLED, !(digitalRead(pinLED)));
        digitalWrite(pinRelay, !(digitalRead(pinRelay)));
        
        //beeper tunes borrowed
        int i, duration;
        for (i = 0; i < songLength; i++) {
            duration = beats[i] * tempo; //in milliseconds

            if (notes[i] == ' ') {
                delay(duration);
            } else {
                tone(pinSpeaker, frequency(notes[i]), duration);
                delay(duration);           
            }
            delay(tempo/10);
        }
        //
    }
    if(digitalRead(pinRelay) == LOW) {
        led1.on();
    } else if(digitalRead(pinRelay) == HIGH) {
        led1.off();
    }
  delay(100);
}

BLYNK_WRITE(V1) { //digital button
    if (param.asInt() == 1) {
        delay(100);
        digitalWrite(pinLED, !(digitalRead(pinLED)));
        digitalWrite(pinRelay, !(digitalRead(pinRelay)));
        int i, duration;
        for (i = 0; i < songLength; i++) {
            duration = beats[i] * tempo; //in milliseconds

            if (notes[i] == ' ') {
                delay(duration);
            } else {
                tone(pinSpeaker, frequency(notes[i]), duration);
                delay(duration);           
            }
            delay(tempo/10);
        }
    }
    delay(100);
    Blynk.notify("PUBLISHED");
}

BLYNK_WRITE(V2) { //timed toggle
    if (param.asInt() == 1) {
        Blynk.notify("Timer Start");
        delay(100);
        //digitalWrite(pinLED, !(digitalRead(pinLED)));
        digitalWrite(pinRelay, !(digitalRead(pinRelay)));
        int i, duration;
        for (i = 0; i < songLength; i++) {
            duration = beats[i] * tempo; //in milliseconds

            if (notes[i] == ' ') {
                delay(duration);
            } else {
                tone(pinSpeaker, frequency(notes[i]), duration);
                delay(duration);           
            }
            delay(tempo/10);
        }
    }
    delay(100);
    Blynk.notify("Timer End");
}

BLYNK_WRITE(V3) { //digital sleep timer
    Blynk.notify("Timer Set");
    if(param.asInt() != 0) {
        digitalWrite(pinRelay, LOW);
        delay(param.asInt()*60000);//delay(param.asInt()*60000); //1 min = 60 sec = 60000 ms
        digitalWrite(pinRelay, HIGH);
    }
    Blynk.virtualWrite(V3, 0);
    Blynk.notify("Timer Done");
}
/*
BLYNK_WRITE(V4) {
    WidgetLED led1(V4);
    if(digitalRead(pinRelay) == LOW) {
        led1.on();
    } else {
        led1.off();
    }
}
*/
BLYNK_WRITE(V5) { //ABORT 
    if(param.asInt() == 1) {
        digitalWrite(pinRelay, HIGH);
        Blynk.notify("ABORTED");
    }
}

int frequency(char note) 
{
  int i;
  const int numNotes = 1000;
  /*
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  */
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'x', 'a', 'z', 'b', 'C', 'y', 'D', 'w', 'E', 'F', 'q', 'G', 'i' };
  // c=C4, C = C5. These values have been tuned
  int frequencies[] = { 1898, 1690, 1500, 1420, 1265, 1194, 1126, 1063, 1001, 947, 893, 843, 795, 749, 710, 668, 630, 594 };
  for (i = 0; i < numNotes; i++)
  {
    if (names[i] == note)
    {
      return(frequencies[i]);
    }
  }
  return(0);
}

Credits

Nathan Nguyen

Nathan Nguyen

1 project • 1 follower
Just a highschool student exploring engineering!
Thanks to ianklatzco.

Comments

Add projectSign up / Login