Last active
August 29, 2015 14:24
-
-
Save K-ways/24b474f82c31eeea421e to your computer and use it in GitHub Desktop.
Code for student using Arduino YUN with HC-05 bluetooth module, full-color LED, and DHT module. Copyright © 2015 by K-ways & Joseph.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
int val = 0; | |
int redLEDPin = 9; | |
int greenLEDPin = 10; | |
int blueLEDPin = 11; | |
void setup() { | |
pinMode(redLEDPin, OUTPUT); | |
pinMode(greenLEDPin, OUTPUT); | |
pinMode(blueLEDPin, OUTPUT); | |
Serial1.begin(9600); //This is for bluetooth. | |
Serial.begin(9600); //This is for serial monitor. | |
} | |
void loop() { | |
if ( Serial1.available() ) { | |
val = Serial1.read(); | |
Serial.println(val); | |
} | |
switch ( val ) { | |
case '0': | |
analogWrite(redLEDPin, ???); | |
analogWrite(greenLEDPin, ???); | |
analogWrite(blueLEDPin, ???); | |
break; | |
case '1': | |
analogWrite(redLEDPin, ???); | |
analogWrite(greenLEDPin, ???); | |
analogWrite(blueLEDPin, ???); | |
break; | |
case '2': | |
analogWrite(redLEDPin, ???); | |
analogWrite(greenLEDPin, ???); | |
analogWrite(blueLEDPin, ???); | |
break; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
int val = 0; | |
void setup() | |
{ | |
pinMode(13, OUTPUT); | |
digitalWrite(13, LOW); | |
Serial1.begin(9600); //This is for bluetooth. | |
Serial.begin(9600); //This is for serial monitor. | |
} | |
void loop() | |
{ | |
if(Serial1.available()) | |
{ | |
val = Serial1.read(); | |
} | |
switch (val) | |
{ | |
case '0': | |
digitalWrite(13, LOW); | |
break; | |
case '1': | |
digitalWrite(13, HIGH); | |
break; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int redLEDPin = 9; | |
int greenLEDPin = 10; | |
int blueLEDPin = 11; | |
int n = 256; | |
int t = 5; //This is the time changing to the next color. | |
void setup() | |
{ | |
pinMode(redLEDPin, OUTPUT); | |
pinMode(greenLEDPin, OUTPUT); | |
pinMode(blueLEDPin, OUTPUT); | |
analogWrite(redLEDPin, 255); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 0); | |
} | |
void loop() | |
{ | |
upward(greenLEDPin); | |
downward(redLEDPin); | |
upward(blueLEDPin); | |
downward(greenLEDPin); | |
upward(redLEDPin); | |
downward(blueLEDPin); | |
} | |
void upward(int p) | |
{ | |
for(int i=0; i<256; i++) | |
{ | |
analogWrite(p, i); | |
delay(t); | |
} | |
} | |
void downward(int p) | |
{ | |
for(int i=255; i>=0; i--) | |
{ | |
analogWrite(p, i); | |
delay(t); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
int redLEDPin = 9; | |
int greenLEDPin = 10; | |
int blueLEDPin = 11; | |
int mode = 0; | |
int val = 0; | |
void setup() | |
{ | |
pinMode(redLEDPin, OUTPUT); | |
pinMode(greenLEDPin, OUTPUT); | |
pinMode(blueLEDPin, OUTPUT); | |
Serial1.begin(9600); //This is for bluetooth. | |
Serial.begin(9600); //This is for serial monitor. | |
} | |
void loop() | |
{ | |
if(Serial1.available()) | |
{ | |
mode = Serial1.read(); | |
switch (mode) | |
{ | |
case 'r': | |
Serial1.println("Please enter RED amount (from 0-255): "); | |
while(1) | |
{ | |
if(Serial1.available()) | |
{ | |
val = Serial1.read(); | |
Serial1.println("OK"); | |
analogWrite(redLEDPin, val); | |
break; | |
} | |
} | |
break; | |
case 'g': | |
Serial1.println("Please enter GREEN amount (from 0-255): "); | |
while(1) | |
{ | |
if(Serial1.available()) | |
{ | |
val = Serial1.read(); | |
Serial1.println("OK"); | |
analogWrite(greenLEDPin, val); | |
break; | |
} | |
} | |
break; | |
case 'b': | |
Serial1.println("Please enter BLUE amount (from 0-255): "); | |
while(1) | |
{ | |
if(Serial1.available()) | |
{ | |
val = Serial1.read(); | |
Serial1.println("OK"); | |
analogWrite(blueLEDPin, val); | |
break; | |
} | |
} | |
break; | |
case 'c': | |
Serial1.println("Clear!"); | |
analogWrite(redLEDPin, 0); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 0); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
rgbMix for ThreeForOneCourse | |
*/ | |
int redLEDPin = 9; | |
int greenLEDPin = 10; | |
int blueLEDPin = 11; | |
void setup() { | |
pinMode(redLEDPin, OUTPUT); | |
pinMode(greenLEDPin, OUTPUT); | |
pinMode(blueLEDPin, OUTPUT); | |
} | |
void loop() { | |
//Color1 | |
analogWrite(redLEDPin, ???); | |
analogWrite(greenLEDPin, ???); | |
analogWrite(blueLEDPin, ???); | |
delay(1000); | |
//Color2 | |
analogWrite(redLEDPin, ???); | |
analogWrite(greenLEDPin, ???); | |
analogWrite(blueLEDPin, ???); | |
delay(1000); | |
//Color3 | |
analogWrite(redLEDPin, ???); | |
analogWrite(greenLEDPin, ???); | |
analogWrite(blueLEDPin, ???); | |
delay(1000); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int redLEDPin = 9; | |
int greenLEDPin = 10; | |
int blueLEDPin = 11; | |
void setup() { | |
pinMode(redLEDPin, OUTPUT); | |
pinMode(greenLEDPin, OUTPUT); | |
pinMode(blueLEDPin, OUTPUT); | |
} | |
void loop() { | |
//Red (255,0,0) | |
analogWrite(redLEDPin, 255); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 0); | |
delay(1000); | |
//Green (0,255,0) | |
analogWrite(redLEDPin, 0); | |
analogWrite(greenLEDPin, 255); | |
analogWrite(blueLEDPin, 0); | |
delay(1000); | |
//Blue (0,0,255) | |
analogWrite(redLEDPin, 0); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 255); | |
delay(1000); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include "DHT.h" | |
#define DHTPIN A3 // what pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
DHT dht(DHTPIN, DHTTYPE); | |
int redLEDPin = 9 | |
int greenLEDPin = 10; | |
int blueLEDPin = 11; | |
int mode = 0; | |
void setup() { | |
Serial.begin(9600); | |
Serial1.begin(9600); | |
Serial1.println("DHT11 Connecting successfully!"); | |
pinMode(redLEDPin, OUTPUT); | |
pinMode(greenLEDPin, OUTPUT); | |
pinMode(blueLEDPin, OUTPUT); | |
pinMode(A5,OUTPUT); | |
pinMode(A4,OUTPUT); | |
digitalWrite(A5,LOW); | |
digitalWrite(A4,HIGH); | |
dht.begin(); | |
} | |
void loop() { | |
delay(2000); | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial1.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
if (t > 30 && h <= 50 && mode != 1) { | |
//Red (255,0,0) | |
analogWrite(redLEDPin, 255); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 0); | |
Serial1.println("It's too hot !"); | |
mode = 1; | |
} | |
if (t <= 30 && h > 50 && mode != 2) { | |
//Blue (0,0,255) | |
analogWrite(redLEDPin, 0); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 255); | |
Serial1.println("It's so humid !"); | |
mode = 2; | |
} | |
if (t > 30 && h > 50 && mode != 3) { | |
//purple (255,0,255) | |
analogWrite(redLEDPin, 255); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 255); | |
Serial1.println("It's both hot and humid !"); | |
mode = 3; | |
} | |
if (t <= 30 && h <= 50 && mode != 4) { | |
//Green (0,255,0) | |
analogWrite(redLEDPin, 0); | |
analogWrite(greenLEDPin, 255); | |
analogWrite(blueLEDPin, 0); | |
Serial1.println("It's comfortable now ~"); | |
mode = 4; | |
} | |
float hi = dht.computeHeatIndex(f, h); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include "DHT.h" | |
#define DHTPIN A3 // what pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
// Initialize DHT sensor for normal 16mhz Arduino | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("DHT11 Connecting successfully!"); | |
pinMode(A4,OUTPUT); | |
pinMode(A5,OUTPUT); | |
digitalWrite(A5,LOW); | |
digitalWrite(A4,HIGH); | |
dht.begin(); | |
} | |
void loop() { | |
// Wait a few seconds between measurements. | |
delay(2000); | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
// Compute heat index | |
// Must send in temp in Fahrenheit! | |
float hi = dht.computeHeatIndex(f, h); | |
Serial.print("Humidity: "); | |
Serial.print(h); | |
Serial.print(" % "); | |
Serial.print("Temperature: "); | |
Serial.print(t); | |
Serial.print(" *C "); | |
Serial.print(f); | |
Serial.print(" *F "); | |
Serial.print("Heat index: "); | |
Serial.print(hi); | |
Serial.println(" *F"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include "DHT.h" | |
#define DHTPIN A3 // what pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
// Initialize DHT sensor for normal 16mhz Arduino | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(9600); | |
Serial1.begin(9600); | |
Serial1.println("DHT11 Connecting successfully!"); | |
pinMode(A4,OUTPUT); | |
pinMode(A5,OUTPUT); | |
digitalWrite(A5,LOW); | |
digitalWrite(A4,HIGH); | |
dht.begin(); | |
} | |
void loop() { | |
// Wait a few seconds between measurements. | |
delay(2000); | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial1.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
// Compute heat index | |
// Must send in temp in Fahrenheit! | |
float hi = dht.computeHeatIndex(f, h); | |
Serial1.print("Humidity: "); | |
Serial1.print(h); | |
Serial1.print(" % "); | |
Serial1.print("Temperature: "); | |
Serial1.print(t); | |
Serial1.print(" *C "); | |
Serial1.print(f); | |
Serial1.print(" *F "); | |
Serial1.print("Heat index: "); | |
Serial1.print(hi); | |
Serial1.println(" *F"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include "DHT.h" | |
#define DHTPIN A3 // what pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
DHT dht(DHTPIN, DHTTYPE); | |
int redLEDPin = 9; | |
int greenLEDPin = 10; | |
int blueLEDPin = 11; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("DHT11 Connecting successfully!"); | |
pinMode(redLEDPin, OUTPUT); | |
pinMode(greenLEDPin, OUTPUT); | |
pinMode(blueLEDPin, OUTPUT); | |
pinMode(A5,OUTPUT); | |
pinMode(A4,OUTPUT); | |
digitalWrite(A5,LOW); | |
digitalWrite(A4,HIGH); | |
dht.begin(); | |
} | |
void loop() { | |
delay(2000); | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
if (t > 30 && h <= 50) { | |
//Red (255,0,0) | |
analogWrite(redLEDPin, 255); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 0); | |
} | |
if (t <= 30 && h > 50) { | |
//Blue (0,0,255) | |
analogWrite(redLEDPin, 0); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 255); | |
} | |
if (t > 30 && h > 50) { | |
//purple (255,0,255) | |
analogWrite(redLEDPin, 255); | |
analogWrite(greenLEDPin, 0); | |
analogWrite(blueLEDPin, 255); | |
} | |
if (t <= 30 && h <= 50) { | |
//Green (0,255,0) | |
analogWrite(redLEDPin, 0); | |
analogWrite(greenLEDPin, 255); | |
analogWrite(blueLEDPin, 0); | |
} | |
float hi = dht.computeHeatIndex(f, h); | |
Serial.print("Humidity: "); | |
Serial.print(h); | |
Serial.print(" % "); | |
Serial.print("Temperature: "); | |
Serial.print(t); | |
Serial.print(" *C "); | |
Serial.print(f); | |
Serial.print(" *F "); | |
Serial.print("Heat index: "); | |
Serial.print(hi); | |
Serial.println(" *F"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*====================================================================================== | |
Setup your HC-05 bluetooth module using Arduino YUN. | |
This is quite different from Arduino UNO!! | |
We connect bluetooth module's Rx to Arduino YUN's Tx(D1) and vice versa. | |
Define "serial" for serial monitor on IDE, and define "Serial1" for HC-05. | |
WE MUST USING THE SAME SPEED ON "Serial" and "Serial1"!! | |
So, for the AT setup, we use both 38400 baud as our communication speed. | |
Note. Set the serial monitor as "NL & CR", baud rate "38400". | |
======================================================================================*/ | |
#include <SoftwareSerial.h> | |
void setup() | |
{ | |
Serial1.begin(38400); //This is for bluetooth. | |
Serial.begin(38400); //This is for serial monitor. | |
Serial.println("Enter commands:"); | |
} | |
void loop() | |
{ | |
if (Serial1.available()) //If bluetooth has something to tell arduino... | |
{ | |
char data = Serial1.read(); | |
Serial.print(data); | |
} | |
if (Serial.available()) //If arduino has something to tell bluetooth... | |
Serial1.write(Serial.read()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment