Tag Archives: Homework

Week8 HW: Sensor & Serial Input

[Photocell + Button]

// test button

int ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9};
int upSound[] = {100, 200, 300, 400, 500, 600, 700, 800};
int downSound[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
int speaker = 10;
int button1 = 12;
int button2 = 11;
int lightPin = 0;

void setup(){
Serial.begin(9600);
pinMode(speaker, OUTPUT);
for(int i =0; i<=7; i++){
pinMode(ledPin[i], OUTPUT);
}
pinMode(speaker, OUTPUT);
pinMode(button1, INPUT);
}

void loop(){
int lightLevel = analogRead(lightPin);
lightLevel = map(lightLevel, 0, 1023, 0, 10);
Serial.println(lightLevel);

int buttonState1 = digitalRead(button1);
int buttonState2 = digitalRead(button2);

if(buttonState1 == LOW){
for(int i = 0; i<= 7; i++){
analogWrite(ledPin[i], lightLevel);
if(lightLevel>= 1 && lightLevel < 3){
digitalWrite(ledPin[i], HIGH);
tone(speaker, upSound[i]);
delay(20);
digitalWrite(ledPin[i], LOW);
}else if(lightLevel >= 3 && lightLevel < 4){
digitalWrite(ledPin[i], HIGH);
tone(speaker, upSound[i]);
delay(200);
digitalWrite(ledPin[i], LOW);
}else if(lightLevel >= 4 && lightLevel <= 5){
digitalWrite(ledPin[i], HIGH);
tone(speaker, upSound[i]);
delay(800);
digitalWrite(ledPin[i], LOW);
}
}
}else if(buttonState1 == HIGH){
noTone(speaker);
}

if(buttonState2 == LOW){

if(lightLevel>= 0 && lightLevel < 2){
for(int i = 7; i>= 0; i–){
analogWrite(ledPin[i], lightLevel);
digitalWrite(ledPin[i], HIGH);
tone(speaker, downSound[i]);
delay(20);
digitalWrite(ledPin[i], LOW);
}
}else if(lightLevel >=2 && lightLevel<4){
for(int i = 7; i>= 0; i–){
analogWrite(ledPin[i], lightLevel);
digitalWrite(ledPin[i], HIGH);
tone(speaker, downSound[i]);
delay(300);
digitalWrite(ledPin[i], LOW);
}
}else if(lightLevel >=4 && lightLevel <=5){
for(int i = 7; i>= 0; i–){
analogWrite(ledPin[i], lightLevel);
digitalWrite(ledPin[i], HIGH);
tone(speaker, downSound[i]);
delay(1000);
digitalWrite(ledPin[i], LOW);
}
}

}else if(buttonState2 == HIGH){
noTone(speaker);
}
}

//void up(){
// for(int i = 0; i<= 7; i++){
// digitalWrite(ledPin[i], HIGH);
// tone(speaker, upSound[i]);
// delay(200);
// digitalWrite(ledPin[i], LOW);
// delay(200);
// }
//
//}
//
//void down(){
// for(int i = 7; i>= 0; i–){
// digitalWrite(ledPin[i], HIGH);
// tone(speaker, downSound[i]);
// delay(200);
// }
// for(int i =0; i <= 7; i++){
// digitalWrite(ledPin[i], LOW);
// }
// }

[Serail Input]

I definitely gave Soohyun a credit! Her code is more flexible. You can create your own melody by typing letters.

I had a little problem about “empty character constant” so I goologed the answer: ” means empty.

Here is my reference:http://www.codeproject.com/Questions/154469/Help-me-with-empty-character-constant-error

int ledPin[] = {6, 7, 8, 9};
int buzzerPin = 10;
int incomingByte = 0;

void setup(){
Serial.begin(9600);
for(int i = 0; i < 4; i++){
pinMode(ledPin[i], OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
}

void loop(){
if (Serial.available() > 0) {
incomingByte = Serial.read();
if(incomingByte == ”){ //” means empty
delay(500);
}else if(incomingByte == ‘c’){
digitalWrite(ledPin[0], HIGH);
tone(buzzerPin, 262);
delay(300);
noTone(buzzerPin);
digitalWrite(ledPin[0], LOW);
}else if(incomingByte == ‘d’){
digitalWrite(ledPin[1], HIGH);
tone(buzzerPin, 294);
delay(300);
noTone(buzzerPin);
digitalWrite(ledPin[1], LOW);
}else if(incomingByte == ‘e’){
digitalWrite(ledPin[2], HIGH);
tone(buzzerPin, 330);
delay(300);
noTone(buzzerPin);
digitalWrite(ledPin[2], LOW);
}else if(incomingByte == ‘f’){
digitalWrite(ledPin[3], HIGH);
tone(buzzerPin, 349);
delay(300);
noTone(buzzerPin);
digitalWrite(ledPin[3], LOW);
}
}
}

Leave a comment

Filed under Uncategorized

Arduino HW!!

Image

//HW 3 LEDS

int L1 = 2;
int L2 = 3;
int L3 = 4;

void setup(){
Serial.begin(9600);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
}

void loop(){
digitalWrite(L1, HIGH);
delay(200);
digitalWrite(L1, LOW);
delay(300);
digitalWrite(L2, HIGH);
delay(100);
digitalWrite(L2, LOW);
delay(500);
digitalWrite(L3, HIGH);
delay(400);
digitalWrite(L3, LOW);
delay(200);

}

Image

 

 

//HW 3 LEDS & 2 Buttons

int L1 = 2;
int L2 = 3;
int L3 = 4;

int button1 = 5;
int button2 = 6;

int stateB1 = 0;
int stateB2 = 0;

void setup(){
Serial.begin(9600);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);

pinMode(button1, INPUT);
pinMode(button2, INPUT);

digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
}

void loop(){
int stateB1 = digitalRead(button1);
int stateB2 = digitalRead(button2);

if(stateB1 == 1){
digitalWrite(L1, LOW);
digitalWrite(L2, LOW);
digitalWrite(L3, LOW);

}else if(stateB1 == 0){
digitalWrite(L1, LOW);
delay(200);
digitalWrite(L1, HIGH);
delay(200);
digitalWrite(L2, HIGH);
delay(200);
digitalWrite(L3, HIGH);
delay(200);

}
//Serial.println(stateB1);
if(stateB2 == 1){
digitalWrite(L1, LOW);
digitalWrite(L2, LOW);
digitalWrite(L3, LOW);

}else if(stateB2 == 0){
digitalWrite(L1, LOW);
delay(200);
digitalWrite(L1, HIGH);
delay(200);
digitalWrite(L3, HIGH);
delay(200);
digitalWrite(L2, HIGH);
delay(200);
digitalWrite(L1, HIGH);
digitalWrite(L2, HIGH);
digitalWrite(L3, HIGH);
delay(200);
digitalWrite(L1, LOW);
digitalWrite(L2, LOW);
digitalWrite(L3, LOW);
delay(200);
digitalWrite(L1, HIGH);
digitalWrite(L2, HIGH);
digitalWrite(L3, HIGH);
delay(200);
}

}

Leave a comment

Filed under Uncategorized

#HW: API Susan

I did the very simple version but the review class last week helped. To me, API is more interesting than Canvas, and also more useful, I guess.  I would love to learn more and use it in the future.

 

http://a.parsons.edu/~shuyl105/#3%20Api.html

Image

<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>JSON WITH API: Astronomy 1</title>

<script type=”text/javascript” src=”js/jquery-1.10.2.min.js”></script>

<script type=”text/javascript”>

/*Standard request URL format =
http://api.wunderground.com/api/b79e3040f6431025/features/settings/q/query.format
where
features=> type of data, here astronomy
settings=> lang or personal data
query=> location of data
*/

$(document).ready(function(){

$.ajax({
url: “http://api.wunderground.com/api/beaf6860b4d9bce1/conditions/q/CA/San_Francisco.json&#8221;,
dataType : “jsonp”, //jsonp for cross-origin files
//put you key into it>>to know who you are

success : function(parsed_json) {
console.log(‘success!!’);
//each nested object goes in []
var location = parsed_json[‘current_observation’][‘display_location’][‘full’];
var latitude = parsed_json[‘current_observation’][‘display_location’][‘latitude’];
var longitude = parsed_json[‘current_observation’][‘display_location’][‘longitude’];
var weather = parsed_json[‘current_observation’][‘weather’];
var temper = parsed_json[‘current_observation’][‘temp_c’];
var humidity = parsed_json[‘current_observation’][‘relative_humidity’];
var UV = parsed_json[‘current_observation’][‘UV’];

 

//var sunset = parsed_json[‘moon_phase’][‘sunset’][‘hour’];
console.log(“Location= ” + location);
console.log(“Latitude= ” + latitude);
console.log(“Longitude= ” + longitude);
console.log(“Weather= ” + weather);
console.log(“Temper= ” + temper);
console.log(“Humidity= ” + humidity);
console.log(“UV= ” + UV);

//console.log(parsed_json); //great debugging tool here!

$(“#Location”).append(“Location: “+location);
$(“#Latitude”).append(“Latitude: “+latitude);
$(“#Longitude”).append(“Longitude: “+longitude);
$(“#Weather”).append(“Weather: “+weather);
$(“#Temperature”).append(“Temperature: “+temper+” C”);
$(“#Humidity”).append(“Humidity: “+humidity);
$(“#UV”).append(“UV: “+UV);

} //end of success
}); //end of ajax
}); //end of doc ready

</script>

</head>

<body>
<ul>
<li id=”Location”></li>
<li id=”Latitude”></li>
<li id=”Longitude”></li>
<li id=”Weather”></li>
<li id=”Temperature”></li>
<li id=”Humidity”></li>
<li id=”UV”></li>
</ul>

</body>
</html>

 

Leave a comment

Filed under Uncategorized

API Homework

Finally getting the hang of this. Thank you for going over it again Master P, really helped.
For anyone in the class who missed it and is looking for it here is what we went over in the review friday:

http://cssdeck.com/labs/ke0mp2in

Here is the code, I got my own key and pulled the weather forecast for my Home town of Wagram North Carolina:

Raw Data link:

http://api.wunderground.com/api/dc48a9b9b5d76b6a/forecast/q/NC/Wagram.json

Script:

<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>JSON WITH API: Astronomy 1</title>

<script type=”text/javascript” src=”js/jquery-1.10.2.min.js”></script>

<script type=”text/javascript”>

/*Standard request URL format =
http://api.wunderground.com/api/b79e3040f6431025/features/settings/q/query.format
where
features=> type of data, here astronomy
settings=> lang or personal data
query=> location of data
*/

$(document).ready(function(){
$.ajax({
url: “http://api.wunderground.com/api/dc48a9b9b5d76b6a/forecast/q/NC/Wagram.json&#8221;,
dataType : “jsonp”, //jsonp for cross-origin files
success : function(parsed_json) {
console.log(‘success!!’);
//each nested object goes in []
var wagramWeather = parsed_json[‘forecast’][‘txt_forecast’][‘forecastday’][1][‘fcttext’];
var wagramWeatherday2 = parsed_json[‘forecast’][‘txt_forecast’][‘forecastday’][3][‘fcttext’];

$(“#container”).append(“Here is the forecast for the place I was born and raised(Wagram, NC): “+wagramWeather);

$(“#container2”).append(“Wednesday in Wagram will be: “+wagramWeatherday2);

} //end of success
}); //end of ajax
}); //end of doc ready

 
</script>

</head>

<body>
<section id=”container”>
<
</section>
<section id=”container2″>

</section>
</body>
</html>

Leave a comment

Filed under Homework

#2 HW2 Canvas Animation

I really can’t fully understand canvas, I did a lot of research but it wasn’t enough.  Thank Soohyun for teaching me line by line to let me understand how canvas works.  Definitely give her credit! I love you!

My homework is not prefect at all but I am very happy I can finally make something MOVE in a cool way. I hope it’s creative enough.

I learned a lot from this homework, like the logic of canvas. However, I still have a problem to make an array for animation. Now, I just copy and paste three times in code to make three boxes move.  I want to know how to do it in my work.

Reference:
1. Basic: http://www.sitepoint.com/basic-animation-with-canvas-and-javascript/
2. Soohyun’s homework
3
. Array in canvas: http://stackoverflow.com/questions/10513480/html5-canvas-array-of-images-draw-image-to-canvas

ImageImageImageImage

Leave a comment

Filed under Uncategorized

Week1 Madlib HW

<!DOCTYPE html>
<html>
<head>
<title> My Basic HTML</title>
</head>
<body>
<script type=”text/javascript”>
// this is where javascript lives
// console.log(“This is javascripts println();”);
var namex = “Chris”;
var person = “Batman”;
var place = “The Phantom Zone”;
var day = “On Hump Day”;
var action1 = “rescue “;
var action2 = ” threw “;
var thing = “money”;
var adj = “ballerific”;
var a1 = “Hello, my name is “;
var p1 = “. “;
var p2 = “! “;
var b1 = ” I Went to go see my friend and personal confidant “;
var c1 = ” was very “;
var c2 = ” and felt like partying in “;
var d1 = “After we got there I had to “;
var d2 = ” because they partyed to hearty!”;
var e1 = “Finally, “;
var e2 = ” all of their “;
var e3 = ” away because they wanted to go home.”;
console.log(a1 + namex + p1 + day + b1 + person + p1 + person + c1 + adj + c2 + place + p2 + d1 + action1 + person + d2 + e1 + person + action2 + e2 + thing + e3);
</script>

</body>
</html>

1 Comment

Filed under Homework