Wednesday, July 1, 2015

Keyboard Controlled Lights

How to build and control an LED on an Arduino using a keyboard:
by J.B. Wylzan


Project 19:  Keyboard controlled LED
This project turns On and Off  an Led using a keyboard thru the Arduino Serial Monitor.


Block Diagram:


Hardware:
1 Keyboard
1 led
1 resistor
connecting wires
breadboard
Arduino R3 UNO board
Arduino Serial Monitor

Code # 19:
/*
  iHacklab Keyboard Controlled Led
  powered by Arduino

  Turns On and Off  an Led using the keyboard and Arduino Serial Monitor
  The example code is public domain.
 */

int ledPin = 8; 
int state = 0;
int flag = 0; 

void setup() {
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);
 Serial.begin(9600);                         //  Setup Serial Monitor
}

void loop() {

 if(Serial.available() > 0){
 state = Serial.read();
 flag=0;
 }

 if (state == '0') {
 digitalWrite(ledPin, LOW);
 if(flag == 0){
 Serial.println("LED: off");
 flag = 1;
 }
 }

 else if (state == '1') {
 digitalWrite(ledPin, HIGH);
 if(flag == 0){
 Serial.println("LED: on");
 flag = 1;
 }
 }
}

// end of the sketch

New Command: Serial.Println



Challenge: 
Use the keyboard to light up 4 leds and display each status in the arduino serial monitor.

Actual Layout:




Procedure:
1. Build the prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #19 above
5. Paste Code #19
6. Click File > Save
7. Click Verify
8. Click Upload
9. Open the Serial Monitor
10. Type 1 to turn on the Led
11. Type 0 to turn off the Led


Project # 21:  Motorshield L239D


Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.

================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®

L.A.W.S.I.N. Educational Production
 ================================================================== 






No comments:

Post a Comment