The big idea of this project is to find alternative ways to shutdown / start up computers. I’ve already thought about clap-on, clap off toggle, but the sensor I was trying it with was bad. It didn’t give much or any changes in readings when sound was short. When sound was continious it gave little changes.
So to this project. The FlashlightToggle works like this: blink a flashlight to the photoresistor 5 times. Then python-serial reads the value serial is giving and runs shutdown command, computer shuts down. If you only flash light one to four times, nothing happens and if you wait for 20 seconds of the first flashing, it resets.
Testing arduino:
ALWAYS before starting a new project, one must test arduino with hello world (blink). Check my earlier blog posts for this. Never skip it.
Installing Python and pySerial
To command line in Ubuntu 12.04:
sudo apt-get update sudo apt-get install python sudo apt-get install python-serial
Testing:
So to test out, I’m going to insert my arduino to testing machine, type:
sudo python
and write down:
import serial ser = serial.Serial('/dev/ttyACM0', 9600) while True: print ser.readline()
then after last line is written, unix command line starts printing arduino serial monitor
Example image from this project:
Tools and components:
Components:
Arduino UNO 3.0 (basicly any Arduino should fit)
Photoresistor
USB-cable
5x 220 Ohm resistors
1x 10k Ohm resistor
several jumper wires
Sketch:
Here is beautiful sketch made by me. Simply connect leds to digital ports and ground through 220 ohms. Photoresistor need to have 10k ohm to ground, and between photoresistor and 10k you wire jumperwire to analog port. Also you have to give it 5v power.
Tools:
I used Arduino IDE for coding and to insert my codes to Arduino.
Arduino C++ code:
const int gled1 = 13; const int yled1 = 12; const int yled2 = 11; const int rled1 = 10; const int rled2 = 9; const int lightsensor = 0; double sensorreading = 0.0; int rbit2 = 0; int rbit1 = 0; int ybit2 = 0; int ybit1 = 0; int gbit1 = 0; int time; int x = 0; int target = 0; void setup() { Serial.begin(9600); Serial.println("Serial started:"); pinMode(gled1, OUTPUT); pinMode(yled1, OUTPUT); pinMode(yled2, OUTPUT); pinMode(rled1, OUTPUT); pinMode(rled2, OUTPUT); } void loop() { sensorreading = analogRead(lightsensor) / 1023.0; Serial.println(sensorreading); if(sensorreading > 0.88 && rbit2 == 0){ digitalWrite(rled2, HIGH); delay(500); rbit2 = 1; } sensorreading = 0.00; sensorreading = analogRead(lightsensor) / 1023.0; if(sensorreading > 0.88 && digitalRead(rled2) && rbit1 == 0){ digitalWrite(rled1, HIGH); delay(500); rbit1 = 1; } sensorreading = 0.00; sensorreading = analogRead(lightsensor) / 1023.0; if(sensorreading > 0.88 && digitalRead(rled1) && ybit2 == 0){ digitalWrite(yled2, HIGH); delay(500); ybit2 = 1; } sensorreading = 0.00; sensorreading = analogRead(lightsensor) / 1023.0; if(sensorreading > 0.88 && digitalRead(yled2) && ybit1 == 0){ digitalWrite(yled1, HIGH); delay(500); ybit1 = 1; } sensorreading = 0.00; sensorreading = analogRead(lightsensor) / 1023.0; if(sensorreading > 0.88 && digitalRead(yled1) && gbit1 == 0){ digitalWrite(gled1, HIGH); delay(500); gbit1 == 1; lightshow(); } delay(100); wait(); } void wait(){ time = millis()/1000; while(x == 0){ if(rbit1 == 1 || rbit2 == 1 || ybit1 == 1 || ybit2 == 1 || gbit1 == 1){ target = time + 20; x++; } x++; } if(time >= target){ digitalWrite(gled1,LOW); digitalWrite(yled1,LOW); digitalWrite(yled2,LOW); digitalWrite(rled2,LOW); digitalWrite(rled1,LOW); rbit1 = 0; rbit2 = 0; ybit1 = 0; ybit2 = 0; gbit1 = 0; x=0; } } void lightshow(){ digitalWrite(gled1,HIGH); delay(50); digitalWrite(yled2,HIGH); delay(50); digitalWrite(yled1,HIGH); delay(50); digitalWrite(gled1,LOW); digitalWrite(rled2,HIGH); delay(50); digitalWrite(rled1,HIGH); digitalWrite(yled2,LOW); digitalWrite(gled1,HIGH); delay(50); digitalWrite(yled2,HIGH); delay(50); digitalWrite(yled1,HIGH); delay(50); digitalWrite(gled1,LOW); digitalWrite(rled2,HIGH); delay(50); digitalWrite(rled1,HIGH); digitalWrite(yled2,LOW); digitalWrite(gled1,LOW); digitalWrite(rled2,HIGH); delay(50); digitalWrite(rled1,HIGH); digitalWrite(yled2,LOW); digitalWrite(yled2,HIGH); delay(50); digitalWrite(yled1,HIGH); delay(50); digitalWrite(gled1,LOW); digitalWrite(rled2,HIGH); delay(50); digitalWrite(rled1,HIGH); digitalWrite(yled2,LOW); digitalWrite(gled1,LOW); digitalWrite(rled2,HIGH); delay(50); digitalWrite(rled1,HIGH); digitalWrite(yled2,LOW); delay(50); digitalWrite(gled1,LOW); digitalWrite(yled1,LOW); digitalWrite(yled2,LOW); digitalWrite(rled2,LOW); digitalWrite(rled1,LOW); rbit1 = 0; rbit2 = 0; ybit1 = 0; ybit2 = 0; gbit1 = 0; x=0; Serial.println("SHUTDOWN"); delay(1000); }
Python code:
import subprocess import serial ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1) while True: print ser.readline() line = ser.readline().decode('utf-8')[:-2] if line == 'SHUTDOWN': subprocess.call(["sudo", "shutdown","-h", "now"])
Just save this python code to lets say like shutdown-arduino.py, then run it into screen with
sudo screen python shutdown-arduino.py
and leave screen running with CTRL+A and CTLR+D
Now when flashlight hits the sensor 5 times, computer shuts down
Results in video (sorry about the wibbily wobbilyness, cameraman was little bit tired):
Info:
Computer:
P8-P67 Rev 3.0 motherboard, AMD 6970 HD 2gb grapich card, Intel I5-2500k 6mb cache 4,5 ghz processor, 8gb 1950mhz RAM, and other important components.
Operating System:
Windows 7 64-bit Professional, Service Pack 1
Testcomputer: Old laptop, running on Ubuntu 12.04
Resources:
- Karvinen, Tero: Classes in Spring 2014, Prototyypin rakentaminen -course
- Arduino IDE
- Arduino Book: Arduino Projects Book (comes with Arduino Starter Kit)
- Arduino and Python, arduino playground
- http://pyserial.sourceforge.net/pyserial_api.html
- http://www.dreamincode.net/forums/topic/298310-new-to-python-serial-readout/