Tuesday, October 27, 2009

TSVETA [ Code 1/6, 2/6, 3/6 and 6/6 ]

Code 1/6

int led = 5;
int buttonpin = 10; //set up pin for the button
int flex = 3; //set up pin for flex sensor/photo cell
int buttonstate = HIGH;


void setup(){
pinMode(led, OUTPUT);
pinMode(buttonpin, INPUT);//buttonpin and flex sensor/photocell pin to be recognized as input
pinMode(flex, INPUT);
Serial.begin(9600);
}


void loop(){
int state = analogRead(flex); //read analog value from sensor and assign it to state
Serial.println(state);
int button = digitalRead(buttonpin); //
if (button != buttonstate){ //check if state has been changed and if yes
digitalWrite(led, HIGH); // turn led on
delay(state*2); // double the value from analog input and use to determine delay
digitalWrite(led, LOW);//turn led off
delay(state*2);
}

}

Code 2/6


int buttonpin = 10;//set up pin for the button
int photo = 3;//set up pin for photo cell
int buttonstate = HIGH;//initial state for later comparison



void setup(){
for (int thisPin = 2; thisPin <= 7; thisPin++) {
pinMode(thisPin, OUTPUT); //going through the leds to set them as output
}
pinMode(buttonpin, INPUT); //buttonpin and photo pin to be recognized as input
pinMode(photo, INPUT);

Serial.begin(9600);//begin serial port

}

void loop(){

int button = digitalRead(buttonpin); //get the data from the button and assign it to interger button
if (button != buttonstate) { //check if the data is different than the initial setup
int analogvalue = analogRead(photo); //read analog data from photo cell and store it in int analogavalue
Serial.println(analogvalue);
for (int thisPin = 2; thisPin <= 7; thisPin++) { //go through leds from lower number to higher to light them up fluidly


digitalWrite(thisPin, HIGH); // turn the pin on:
delay(analogvalue); // use the analog data to set up delay - dynamically changing
digitalWrite(thisPin, LOW); // turn the pin off:
delay(analogvalue);
//set switch to 0 - stop reading flex
}
delay(20);

for (int thisPin = 7; thisPin >= 2 ; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH); // turn the pin on:
delay(analogvalue); // use the analog data to set up delay - dynamically changing
digitalWrite(thisPin, LOW); // turn the pin off:
delay(analogvalue);
}

}

}


Code 3/6

int led = 10;//set up pin 10 for the button
int photo = 3;//set up pin for flex sensor/photo cell
void setup(){
pinMode(led, OUTPUT);//led pin will be outputing info
pinMode(photo, INPUT);//photocell pin will be receiving input
Serial.begin(9600);//begin serial port }
void loop(){
int delaytime = analogRead(photo); //assign analog data received to temporary variable
for(int i=0; i<= 225; i++){ //use i as a counter
analogWrite(led, i); //output to gradually light up led
delay(delaytime); //use value from photocell to set up delay time, dynamically changing }
for(int i=225; i>=0; i--){
analogWrite(led, i); //output to gradually turn off led
delay(delaytime); } }


Code 6/6


int pinArray[] = { 2, 3, 4 }; //declare first array that includes pins 2 through 4
int pinArray2[] = {5, 6, 7}; //declare a second array that includes pins 5 through 7
int arrayVal = 2; //declare a variable to hold the array values; int arrayVal2 = 0;
int switchPin = 10;
int switchstate = HIGH;
int photo = 3;

void setup() {
pinMode(switchPin, INPUT);
pinMode(photo, INPUT);
for ( arrayVal = 2; arrayVal >= 0; arrayVal-- ){ //use the for statement to go through all numbers from the array - from 4 to 2
pinMode(pinArray[arrayVal], OUTPUT); //set all pins from that array to be output }
for ( arrayVal2 = 0; arrayVal2 <=2 ; arrayVal2++ ){ //use the for statement to go through all numbers from the array - from 5 to 7
pinMode(pinArray2[arrayVal2], OUTPUT); //set all pins from that array to be output }
Serial.begin(9600);
}

void loop() {
int state = digitalRead(switchPin); //check switch state
int analogvalue = analogRead(photo); //take analog values and assign to local variable
Serial.println(analogvalue);
if (state != switchstate && analogvalue ){
arrayval =" 2;">=0; arrayVal--){ //test pins in backwards order (from higher number to lower)
digitalWrite(pinArray[arrayVal], HIGH); //light them up fluidly
delay(30);
digitalWrite(pinArray[arrayVal-1], HIGH);
Delay(30);
digitalWrite(pinArray[arrayVal], LOW);
delay(30);
}
for (arrayVal2 = 0; arrayVal2 <= 2 ; arrayVal2++){ //test pins going from lower number to higher
digitalWrite(pinArray2[arrayVal2], HIGH); //light up led in a fluid manner
delay(30);
digitalWrite(pinArray2[arrayVal2+1], HIGH);
delay(30);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(30);
}
}
if ( state == switchstate ){ //if there has been change in the switch mode
for (arrayVal = 2; arrayVal>=0; arrayVal--){ //test pins in backwards order (from higher number to lower)
digitalWrite(pinArray[arrayVal], HIGH);
delay(analogvalue); //use the analog value received to determine
digitalWrite(pinArray[arrayVal-1], HIGH);
delay(analogvalue);
digitalWrite(pinArray[arrayVal], LOW);
delay(analogvalue);
}
for (arrayVal2 = 0; arrayVal2 <= 2 ; arrayVal2++){ //test pins going from lower number to higher
digitalWrite(pinArray2[arrayVal2], HIGH);
//light up led in a fluid manner
delay(analogvalue);
digitalWrite(pinArray2[arrayVal2+1], HIGH);
delay(analogvalue);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(analogvalue); }
if (state != switchstate && analogvalue > 60){ //if the analog value is more than 60 and switch state hasn't been changed
for (arrayVal = 2; arrayVal>=0; arrayVal--){ //test pins in backwards order (from higher number to lower)
digitalWrite(pinArray[arrayVal], HIGH);
delay(analogvalue);
digitalWrite(pinArray[arrayVal-1], HIGH);
delay(analogvalue);
digitalWrite(pinArray[arrayVal], LOW);
delay(analogvalue);
}
for (arrayVal2 = 0; arrayVal2 <= 2 ; arrayVal2++){ //test pins going from lower number to higher
digitalWrite(pinArray2[arrayVal2], HIGH); //light up led in a fluid manner
delay(100);
digitalWrite(pinArray2[arrayVal2+1], HIGH);
delay(100);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(100);
}
}
/* //////////////////HERE IS WHERE I STILL HAVE ISSUES TO SOLVE BUT THE GOAL IS TO USE SINGLE ANALOG VALUE, LET IT GROW IT OVER TIME AND USE THAT TO GRADUALLY TURN THE LEDS ON FOR A SOFTER FEELING AND LOOK/////////////////////////////////// if (state==switchstate && analogvalue>=200){ int myvalue = analogvalue; for(arrayVal=0; arrayVal<=2; arrayVal++){ //test pins going from lower number to higher for( myvalue =0; myvalue<=255; myvalue++){ analogWrite(pinArray[arrayVal], myvalue); //light up led in a fluid manner delay(20); analogWrite(pinArray[arrayVal+1], 255-myvalue); delay(20); //analogWrite(pinArray[arrayVal], myvalue); //delay(2); } for (arrayVal2 = 2; arrayVal2 >= 0; arrayVal2--){ //test pins going from higher number to lower number digitalWrite(pinArray2[arrayVal2], myvalue); delay(20); digitalWrite(pinArray2[arrayVal2-1], myvalue); delay(20); digitalWrite(pinArray2[arrayVal2], myvalue); delay(20); } } }*/ } }

Shanna B Exercise #3 (Resistor Light Value)

int ledPin = 9; // Declare pin9 for LED
int photoPin = 1; // variable resistor on analog pin 1
int val = 0;

void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT); // set led pin 9 as OUTPUT
pinMode(photoPin,OUTPUT); // set analog pin 1 as OUTPUT
}

void loop() { // run over and over again
val = analogRead(photoPin); // set val equal to the resistor input

if(val == LOW) {
digitalWrite(ledPin, HIGH);
delay(100);

} else {
digitalWrite(ledPin, LOW);
}
Serial.println(val);
}

Sunday, October 18, 2009

PWM LED

int ledPin = 10;
int analogPin = 0;
int analogValue = 0;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(analogPin, OUTPUT);
}


void loop() {
  analogValue = analogRead( analogPin);
  
  analogWrite(ledPin, analogValue);
  
/*  if(analogValue == HIGH){
  analogWrite(ledPin, LOW);
  delay(200);
  }
  
  else
{
  analogWrite(ledPin, HIGH);
}*/
Serial.println(analogValue);
}

D.collins_Homework_Exercise 3

int ledPin = 9; // declare Pin 9
int analogPin = 2; // Analog pin 2

void setup(){} // no setup needed

void loop() //Function read the analog value and set a delay time and controls the speed at which an LED brightens and dims
{
for (int i=100; i<=10; i++) // give value of 255 for variable "i"
// The above "for loop" function starts the integer "i" at 0, and tests to see if i is still less or equal to 255 and if true, increments "i" by 1 and executes the enclosed statements. i++ is the same as i=i +1 or i by +1

{
analogWrite(ledPin, i); // sets led pin 9 brightess level to i
delay(delayVal()); // generate time value and pauses
}
for (int i=255; i>=0; i--) // if i is 255 then i should be 0
{
analogWrite(ledPin, i); // sets led pin 9 brightess level to i
delay(delayVal()); // generate time value and pauses
}
}

int delayVal() // declare variable for delay time value
{
int v; // create temporary variable
v = analogRead(analogPin); // read analog value
v /= 20; // convert 0-1024 to 0-128
return v; // returns final value
}

D.collins_Classwork_Exercise 3

int ledPin = 12; // Declare pin 11 for LED
int photoPin = 3; // variable resistor on analog pin 0
int photoValue = 0;
int boo = 0;

void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT); // set led pin 12 as OUTPUT
pinMode(photoPin,OUTPUT); // set analog pin 3 as OUTPUT
}

void loop() { // run over and over again
photoValue = analogRead(photoPin); // set val equal to the resistor input

boo = (photoValue/ 3) * 40;



Serial.println(boo);
analogWrite(ledPin, boo);
delay(100);
}

LED Array

int timer = 100; 
int ledPins[] = {2,3,4,5,6 }; 
int pinCount = 5; 
int analogPin = 0;
int analogValue = 0;

void setup() {
Serial.begin(9600);
for (int whatPin = 0;  whatPin<5;>
pinMode(ledPins[whatPin], OUTPUT);
}
}
void loop() { 
int analogValue = analogRead(analogPin);

Serial.println(analogValue);
if(analogValue >= 50) {
for (int whatPin = 0;  whatPin<5;>

  digitalWrite(ledPins[whatPin], HIGH);
delay(timer);

digitalWrite(ledPins[whatPin], LOW);
}}
  if(analogValue <= 49) {
for (int whatPin = 5;  whatPin>0; whatPin--) {

digitalWrite(ledPins[whatPin], HIGH);
delay(timer);

digitalWrite(ledPins[whatPin], LOW);
}
}
}

Shanna B Exercise #1 (Resistor Timing)

int led1Pin = 9; // PWM pin 9 for the LED 1
int led2Pin = 10; // PWM pin 10 for the LED 2
int analogPin = 2; // variable resistor on analog pin 2

void setup(){ }

void loop()
{
for (int i=0; i<=255; i++) // ascending value for i
{
analogWrite(led1Pin, i); // sets brightess level to i
delay(delayVal()); // gets time value and pauses
}
for (int i=255; i>=0; i--) // descending value for i
{
analogWrite(led1Pin, i); // sets brightess level to i
delay(delayVal()); // gets time value and pauses
}

for (int i=0; i<=255; i++)
{
analogWrite(led2Pin, i);
delay(delayVal());
}
for (int i=255; i>=0; i--)
{
analogWrite(led2Pin, i);
delay(delayVal());
}
}

int delayVal()
{
int v; // create temporary variable
v = analogRead(analogPin); // read analog value
v /= 10; // this convert 0-1024 to 0-129
return v; // returns final value
}

Shanna B Assignment #2

int led1Pin = 9; // declare pin 9
int led2Pin = 10; // declare pin 10
int switchPin = 2; // the switch is connected to pin 2
int buttonState; // declare a variable to hold the button state

void setup()
{
pinMode(switchPin, INPUT); // Set the switch pin as INPUT
pinMode(led1Pin, OUTPUT); // LED pin 9 as OUTPUT
pinMode(led2Pin, OUTPUT); // LED pin 10 as OUTPUT


}

void loop() {
buttonState = digitalRead(switchPin); // read the initial state
if(buttonState == HIGH) { // LED (wave) if statement
for (int i=0; i<=20; i++) // set loops for 20 times { digitalWrite(led2Pin, LOW); digitalWrite(led1Pin, HIGH); delay(100); digitalWrite(led1Pin, LOW); digitalWrite(led2Pin, HIGH); delay(100); digitalWrite(led2Pin, LOW); } delay(2000); // pauses for 2 seconds } }

Wednesday, October 14, 2009

Shanna B Assignment #1

*/

int ledPin = 13; // LED connected to digital pin 13

int ledPin2 = 8; // LED connected to digital pin 8

int ledPin3 = 7; // LED connected to digital pin 7
const int analogPin = 0; // pin that the sensor is attached to
const int threshold = 100; // an arbitrary threshold level that's in the range of the analog input


// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);

pinMode(ledPin2, OUTPUT);

pinMode(ledPin3, OUTPUT);

// initialize serial communications:
Serial.begin(9600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(50); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(50); // wait for a second

digitalWrite(ledPin2, HIGH); // set the LED on
delay(8); // wait for a second
digitalWrite(ledPin2, LOW); // set the LED off
delay(8); // wait for a second

digitalWrite(ledPin3, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin3, LOW); // set the LED off
delay(200); // wait for a second

// read the value of the potentiometer:
int analogValue = analogRead(analogPin);

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
delay(250);
}
else {
digitalWrite(ledPin,LOW);
delay(20);
}

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin2, HIGH);
delay(375);
}
else {
digitalWrite(ledPin2,LOW);
delay(100);
}

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin3, HIGH);
delay(20);
}
else {
digitalWrite(ledPin3,LOW);
delay(700);
}

// print the analog value:
Serial.println(analogValue, DEC);
}

Shanna B Assignment #3

int ledPin = 9; // declare Pin 9
int analogPin = 2; // Analog pin 2

void setup(){} // no setup needed

void loop() //Function read the analog value and set a delay time and controls the speed at which an LED brightens and dims
{
for (int i=0; i<=300; i++) // give value of 255 for variable "i" // The above "for loop" function starts the integer "i" at 0, and tests to see if i is still less or equal to 255 and if true, increments "i" by 1 and executes the enclosed statements. i++ is the same as i=i +1 or i by +1 { analogWrite(ledPin, i); // sets led pin 9 brightess level to i delay(delayVal()); // generate time value and pauses } for (int i=300; i>=0; i--) // if i is 255 then i should be 0
{
analogWrite(ledPin, i); // sets led pin 9 brightess level to i
delay(delayVal()); // generate time value and pauses
}
}

int delayVal() // declare variable for delay time value
{
int v; // create temporary variable
v = analogRead(analogPin); // read analog value
v /= 50; // convert 0-1024 to 0-128
return v; // returns final value
}

Tuesday, October 13, 2009

Putra's 3 Exercises



Variable Resistor: Light Value

int ledPin = 11; // Declare pin 11 for LED
int photoPin = 0; // variable resistor on analog pin 0
int val = 0;

void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT); // set led pin 11 as OUTPUT
pinMode(photoPin,OUTPUT); // set analog pin 0 as OUTPUT
}

void loop() {
// run over and over again
val = analogRead(photoPin); // set val equal to the resistor input

if(val == LOW) {
digitalWrite(ledPin, HIGH);
delay(100);

} else {
digitalWrite(ledPin, LOW);
}
Serial.println(val);
}




Variable Resistor: LED Array

int timer = 100; // Number for the time - the higher the number, the slower the timing.
int ledPins[] = {8,9,10,11,12 }; // an array of pin numbers to which LEDs are attached
int pinCount = 5; // the number of pins
int analogPin = 0; // the pin that the potentiometer is attached to
int analogValue = 0;

void setup() { // loop over the pin array and set them all to output:
int thisPin;
pinMode(analogPin, INPUT);
for (int thisPin = 0; thisPin < style="color: rgb(102, 51, 0);">// the array elements are numbered from 0 to (pinCount - 1).
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest
int analogValue = analogRead(analogPin);
if(analogValue >= 50) {
for (int thisPin = 0; thisPin < style="color: rgb(102, 51, 0);">// loop from the highest pin to the lowest
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
}






Variable Resistor: Timing


int led1Pin = 9; // PWM pin 9 for the LED 1
int led2Pin = 11; // PWM pin 11 for the LED 2
int analogPin = 0; // variable resistor on analog pin 0

void setup(){ }

void loop()
{
for (int i=0; i<=255; i++) // ascending value for i
{
analogWrite(led1Pin, i); // sets brightess level to i
delay(delayVal()); // gets time value and pauses
}
for (int i=255; i>=0; i--) // descending value for i
{
analogWrite(led1Pin, i); // sets brightess level to i
delay(delayVal()); // gets time value and pauses
}

for (int i=0; i<=255; i++)
{
analogWrite(led2Pin, i);
delay(delayVal());
}
for (int i=255; i>=0; i--)
{
analogWrite(led2Pin, i);
delay(delayVal());
}
}

int delayVal()
{
int v; // create temporary variable
v = analogRead(analogPin); // read analog value
v /= 7; // this convert 0-1024 to 0-128
return v; // returns final value
}

Andrew Zahn Number Two

This is my movie and code - but I could not get it running - Will check in class


int ledPins[] = {1,2,3,4};
int potPin = 1;
int potValue = 1;
int timer = 100;

void setup() {
  
int whatPin;

for (int thisPin = 0; thisPin;) 

pinMode(ledPins[whatPin], OUTPUT);
}




void loop() {

potValue = analogRead(potPin);
int Timer1;
int Timer2;


if(potValue >= 50) {
  
for (int whatPin = 0; whatPin  >

digitalWrite(ledPins[whatPin], HIGH);
delay(newTimer1);
digitalWrite(ledPins[whatPin], LOW);

}
}


if(potValue <= 49) {
for (int whatPin = pinCount - 1; whatPin >= 0; whatPin--) {
  

digitalWrite(ledPins[whatPin], HIGH);
delay(Timer2);
digitalWrite(ledPins[whatPin], LOW);

}
}
}

Andrew Zahn Number Two

Andrew Zahn Number One


int potpin = 0;
int ledpin = 11; 
int ledvalue = 0;
int potvalue = 0;

void setup(){

pinMode(ledpin, OUTPUT);

}


void loop(){
  
  potvalue =analogRead(potpin);
  ledvalue = potvalue / 4;
  analogWrite(ledpin, ledvalue);
  delay(1);
}

Monday, October 12, 2009

John's 10/14 Assignments

Assignment #1:

Video:


Code:


Assignment #2:

Video:


Code:


Assignment #3:

Video:


Code:
(Shout out to Kyle who helped me out with this.)

Friday, October 9, 2009

Exercise 3

Exercise 3







First, i give variables for the LED pin, the photo pin, and the 2 values. Then i set up 6 variables equal to eventually 6 different blinking patterns. in the void setup i set the LED pin to output, and the serial Statement. Now, in the void loop, i set photoValue to equal to analog read the photo pin when called. Light value will equal photovalue divided by 4 so that it can be read in between 0 and 8 i believe(depending on the resistor). Now, analog write the led pin to the photoValue, and print this. the next are 6 if statements saying if it is reading 1, do this speed, if 2, do this speed, and so on.. up to 6. Then the delay of 1 so that it does not read it as fast.

Exercise 2

Exercise 2






I must give credit to Joe Cohen for help with some of this Code.

First, i am setting a variable for timer which is 50, then i am making 2 arrays with alternating pins, making 2 pin counts for each, setting the value for photoPin and photoValue, the next to variables will just be my leds that indicates iether if green is blinking, or if red is blinking. Then analogvalue is set to 0. In the void setup i put a for statement that sais thisPin is 0, less then pin count 1 (3 leds) and adding 1. Then, set the red and green LEDs to output, and then the serial statement. In the void loop, i set photoValue to analog red photoPin whenever it is called. Now is where the magic happens! If photoValue is greater then or equal to 300, for thisPin is 0, greater then pinCount1, and adding 1, then digitalWrite the green pins from 1 to 3, if not then turn them off, also lets print this value. If photoValue is less then 299, for thisPin is pinCount2 -1. and is greater then or equal to 0, subtracting 1 (makes it go the other way), then digitalwrite the YellowPins on a timed sequence called timer. print this. variable ledYellowPin are the red LED pins, yellow was to hard to tell the difference between green in the video. If photo value is greater then or equal to 300, turn green pin on, if not, then shut that off and turn red pin on.



Week Two Exercise 1

Exercise #1





First, i am setting up intergers for the led pin, the photo pin, and both values for them (ignore the lightBlink1-6 integers). Now, in the void setup, i am setting up the ledpin to outPut, and set up the serial, to later on print values. here comes the void loop where everythign happens constantly. i am setting photovalue so that every time it is called, it will read photoPin analog-ly which can range in values rather then just HIGH and LOW, (0 and 255) next we are setting lightValue to be photoValue divided by 4 because it dives i believe 1,020 into a scale between 0 to 255 in which it can read. Finally we analog-ly write ledPin to the value of photoValue, and then we print the outcome. (the delay of 1 is so that the computer is not reading it as fast, because there is no point for it to work that hard.)

Thursday, October 8, 2009

Tsveta [ Code 4/6 and 5/6 ]

CODE 4/6 - Lighting LEDs using Array


int ledPin = 3;//declare led pin
int photoPin = 0;//declare the analog reading pin
int photoValue = 0;//declare value to store the analog value
int brightness = 0;//declare variable to store and pass the analog value to the led
void setup() { pinMode(ledPin, OUTPUT);//declare output pin
pinMode(photoPin, INPUT);//declare input pin
Serial.begin(9600); }
void loop() {
photoValue = analogRead(photoPin);//read the analog values from the photocell/flexing sensor
brightness = photoValue;//pass analog value from the analog read varibale to the led brightness variable
Serial.println(brightness);//print value received from the photocell/flexing sensor
analogWrite(ledPin, brightness);//pass the analog value to adjust brightness
delay(200);//remove noise
}

_________________________________________________
--------------------------------------------------------------------------------------

CODE 5/6 - Lighting LEDs using Array
The goal here was to have 3 LEDs in one array and 3 led in a different so I can light them up fast from the center of the row to the ends and when pot turned all the way to the other side then it changes the direction and speed.
The issue was that I could not figure out how to make the two different arrays to be doing the same thing at the same time. That is why I took the i that i used as a delay timer and put actual numbers

int pinArray[] = { 2, 3, 4 }; //declare first array that includes pins 2 through 4
int pinArray2[] = { 5, 6, 7}; //declare a second array that includes pins 5 through 7
int arrayVal = 2; //declare a variable to hold the array values;
int arrayVal2 = 0;
int switchPin = 10;
int switchPin2 = 9;
int switchstate = HIGH;




void setup() {
pinMode(switchPin, INPUT);
pinMode(switchPin2, INPUT);
for ( arrayVal = 2; arrayVal >= 0; arrayVal-- ){ //use the for statement to go through all numbers from the array - from 4 to 2
pinMode(pinArray[arrayVal], OUTPUT); //set all pins from that array to be output
}
for ( arrayVal2 = 0; arrayVal2 <=2 ; arrayVal2++ ){ //use the for statement to go through all numbers from the array - from 5 to 7
pinMode(pinArray2[arrayVal2], OUTPUT); //set all pins from that array to be output
}
}
void loop() {
int state = digitalRead(switchPin);
int state2 = digitalRead(switchPin2);
if ( state != switchstate){
for (arrayVal = 2; arrayVal>=0; arrayVal--){ //test pins in backwards order (from higher number to lower)
//for ( i = 0; i<2000;>
digitalWrite(pinArray[arrayVal], HIGH);
delay(100);
digitalWrite(pinArray[arrayVal-1], HIGH);
delay(100);
digitalWrite(pinArray[arrayVal], LOW);
delay(100);
}
for (arrayVal2 = 0; arrayVal2 <= 2 ; arrayVal2++){ //test pins going from lower number to higher
digitalWrite(pinArray2[arrayVal2], HIGH); //light up led in a fluid manner
delay(100);
digitalWrite(pinArray2[arrayVal2+1], HIGH);
delay(100);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(100);}
}
else{
for(arrayVal=0; arrayVal<=2; arrayVal++){ //test pins going from lower number to higher
digitalWrite(pinArray[arrayVal], HIGH); //light up led in a fluid manner
delay(40);
digitalWrite(pinArray[arrayVal+1], HIGH);
delay(40);
digitalWrite(pinArray[arrayVal], LOW);
delay(40);
}
for (arrayVal2 = 2; arrayVal2 >= 0; arrayVal2--){ //test pins going from higher number to lower number
digitalWrite(pinArray2[arrayVal2], HIGH);
delay(40);
digitalWrite(pinArray2[arrayVal2-1], HIGH);
delay(40);
digitalWrite(pinArray2[arrayVal2], LOW);
delay(40);
}
}
}

Wednesday, October 7, 2009

Joe Cohen's 3 In-Class Exercises

#1


/* Code by Wm. Joe Cohen for
Physical Computing Studio */

int ledPin = 5;
int photoPin = 0;
int photoValue = 0;
int lightValue = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(photoPin, INPUT);
Serial.begin(9600);
}

void loop() {
photoValue = analogRead(photoPin);
lightValue = photoValue;
//lightValue = photoValue / 4;
Serial.println(lightValue);
analogWrite(ledPin, lightValue);
delay(100);
}

______________________________________________________

#2


/* Code by Wm. Joe Cohen for
Physical Computing Studio */

int timer = 50;
int ledPins[] = {
7, 6, 5, 4, 3 };
int pinCount = 5;
int photoPin = 0;
int photoValue = 0;


void setup() {
int thisPin;
for (int thisPin = 0; thisPin <>
pinMode(ledPins[thisPin], OUTPUT);
}
pinMode(photoPin, INPUT);
Serial.begin(9600);
}


void loop() {

photoValue = analogRead(photoPin);

// lighter environment (values of 30+) moves the LED flow one way
if(photoValue >= 30) {
for (int thisPin = 0; thisPin <>
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
Serial.println(photoValue);
}
}


// darker environment (values of 29-) moves the LED flow the opposite way
if(photoValue <= 29) {
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
Serial.println(photoValue);
}
}
}

______________________________________________________

#3


/* Code by Wm. Joe Cohen for
Physical Computing Studio */

int timer = 50;
int ledPins[] = {
7, 6, 5, 4, 3 };
int pinCount = 5;
int photoPin = 0;
int photoValue = 0;

void setup() {
int thisPin;
for (int thisPin = 0; thisPin <>
pinMode(ledPins[thisPin], OUTPUT);
}
pinMode(photoPin, INPUT);
Serial.begin(9600);
}

void loop() {

photoValue = analogRead(photoPin);
int newTimer1;
int newTimer2;

// lighter environment (values of 30+) moves the LED flow one way
// and the LEDs move 10 times slower than 'time'
if(photoValue >= 30) {
for (int thisPin = 0; thisPin <>
newTimer1 = timer * 10;
digitalWrite(ledPins[thisPin], HIGH);
delay(newTimer1);
digitalWrite(ledPins[thisPin], LOW);
Serial.println(photoValue);
}
}


// darker environment (values of 29-) moves the LED flow the opposite way
// and the LEDs move 2 times faster than 'time'
if(photoValue <= 29) {
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
newTimer2 = timer / 2;
digitalWrite(ledPins[thisPin], HIGH);
delay(newTimer2);
digitalWrite(ledPins[thisPin], LOW);
Serial.println(photoValue);
}
}
}

Aysha Homework: 10/7/09

Exercise 1




int ledPin = 11; //sets led pin to 11
int potPin = 0; //sets pot pin to andalog 0
int outValue = 0; //value sent to pin
int potValue = 0; //value given from pot



void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //sets lefPin to output
}

void loop(){

potValue = analogRead(potPin); //reads the value of the pot and sets it
outValue = potValue/4; //sets outValue to potValue divided by 4
analogWrite(ledPin, outValue); //sends the value of pot to led
Serial.println(outValue); //prints the calue being sent to led
delay(1);
}





Exercise 2



/*use an array to light up a series of LED, over a series of time.
Use for loops as the counters for your timer. Use your switch or
variable resistor to change this sequence of lighting the LED's to
light in the opposite direction upon input from the switch/variable
resistor*/

int pinA = 3;
int pinB = 4;
int pinC = 5;

int buttonPin = 2;
int buttonState = 0;

int testLED = 13;

int arrayLED[3] = {pinA, pinB, pinC}; //array for LED pins


void setup(){
//use array to set up pins as output

for(int i = 0; i < buttonstate =" digitalRead(buttonPin);" buttonstate ="="" x =" 0;" buttonstate ="="" y =" 5;"> 5; y--){
digitalWrite(arrayLED[y], HIGH);
delay(300);
digitalWrite(arrayLED[y],LOW);
delay(300);
}
}
}







Exercise 3

Alysha homework 9/30

Exercise 1





Exercise 2




int ledPinA = 5;
int ledPinB = 6;
int ledPinC = 7;
int buttonPin = 2;
int buttonState = 0;

void setup(){
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop(){

buttonState = digitalRead(buttonPin);

if(buttonState == HIGH) {
for(int x=0; x <10; x++){
digitalWrite(ledPinA, HIGH);
delay(100);
digitalWrite(ledPinB, HIGH);
delay(100);
digitalWrite(ledPinC, HIGH);
delay(100);
digitalWrite(ledPinA, LOW);
delay(100);
digitalWrite(ledPinB, LOW);
delay(100);
digitalWrite(ledPinC, LOW);
delay(100);
}
}
if(buttonState == LOW){
digitalWrite(ledPinA, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinC, LOW);
}

delay(1);
}






Exercise 3


int ledpin = 12;

int ledpin = 12;
int buttonpin = 2;

int buttonstate = 0;

void setup(){
pinMode(ledpin, OUTPUT);
pinMode(buttonpin, INPUT);
}

void loop(){

buttonstate = digitalRead(buttonpin);

if(buttonstate == HIGH) {
for(int x = 0; x<10; x++){
digitalWrite(ledpin, HIGH);
delay(500);
digitalWrite(ledpin, LOW);
delay(500);
}
}
delay(1);
}

John's 10/7 Homework

Assignment #1

Video:


Code:

//This code determines the amount of time an LED is lit depending on the amount
// of light a photocell picks up

//We begin by setting a few different int variables. First the LED pin, which is 3,
// the button pin, which is 7, and then the pin for photocell, which is 0
int ledPin = 3;
int buttonPin = 7;
int photoPin = 0;

//Then we set the value of the photocell and the button state, both of which
// will be 0 for the time being. We also set the value of the last button state
// to LOW, meaning the button is not being pressed
int photoValue = 0;
int buttonState = 0;
int lastButtonState = LOW;

// Next we set up some variables which will be used for the debounce. These
// are long's, not int's, because they are going to be measuring milliseconds
// which will quickly become to big to be ints. The debounce delay is set to 50
// for no specific reason other than that it works best
long lastDebounceTime = 0;
long debounceDelay = 50;

//Now we begin the setup, this only occurs once
void setup() {
//In the set up, the LED pin mode is set to OUTPUT while the button and photo
// pin modes are set to INPUT
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(photoPin, INPUT);
//This next bit of code simply allows the print and println's to work
Serial.begin(9600);
}


//Now we begin the loop, this occurs over and over again
void loop() {
//At the beginning of the loop we create a new int, light time (which will
// be the amount of time the LED is lit) and we set it equal to the photo value
// times 100 (so that it will light long enough for us to see it)
int lightTime = photoValue * 100;
//We then digitally read a new int called reading as the button pin
int reading = digitalRead(buttonPin);
//The button state is then also digitally read as the button pin
buttonState = digitalRead(buttonPin);
//We now begin an if statement, which only will occur is the button is pressed
if(buttonState == HIGH) {
//In this case, the photo value is analog read as the photo pin
photoValue = analogRead(photoPin);
//We print the photo value to the serial monitor so we can track changes
Serial.println(photoValue);
}
//These other if statements are for the debounce
//If the reading does not equal the last button state (low)...
if (reading != lastButtonState) {
// ... the last debounce time will be set to the current time
lastDebounceTime = millis();
}
//If the current time minus the last debounce time is greater than the
// debounce delay...
if((millis() - lastDebounceTime) > debounceDelay) {
// ... then the button state is set equal to the reading
buttonState = reading;
}
//Finally, at the end of the loop, the LED pin is digitally written to be
// equal to the value of the button state
digitalWrite(ledPin, buttonState);
//There is then a delay equal to the light time
delay(lightTime);
// and the LED is turned off (LOW)
digitalWrite(ledPin, LOW);
//The last button state is then saved as the reading for the next loop around
lastButtonState = reading;
}

Assignment #2

Video:


Code:

//This code lights up 3 LED's using if statements and a for loop

//Begin by setting 5 ints, three for LED pins (13, 10, and 7) and one for the
// button pin (3) as well as for the button state, which determines whether
// or not the button is pressed.
int ledPinOne = 13;
int ledPinTwo = 10;
int ledPinThree = 7;
int buttonPin = 3;
int buttonState = 0;

//Setup the pin modes of the 3 LED's to OUTPUT and the button pin to INPUT
void setup(){
pinMode(ledPinOne, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
//Digitally read the button pin and set it equal to the button state
buttonState = digitalRead(buttonPin);
//Set up an IF statement which determines if the button is pressed.
if(buttonState == HIGH) {
//Set up a for loop which loops a 20 times
for (int i = 0; i <= 20; i++) {
//Digitally write the LED pins a bunch of times to make a neat light show
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
delay(800);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
delay(800);
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
delay(800);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
delay(800);
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, HIGH);
delay(800);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
delay(800);
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
delay(600);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
delay(600);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
delay(600);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
delay(600);
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
delay(800);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
delay(800);
}

}
// If the button is not pressed, set the LED to low (off).
else {
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
}
}

Assignment #3

Video:


Code:

//This code lights up one LED using if statements and a for loop

//Begin by creating 3 int variables. One for the LED connected to pim 13,
// one for the button connected to pin 2, and one for the button state which
// determines whether or not the button is pressed.

int ledPin = 13;
int buttonPin = 2;
int buttonState = 0;

// Next setup; setting the pin mode of the LED pin to OUTPUT, and the pin
// mode of the button pin to INPUT.

void setup(){
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

//Digitally read the button pin and set it equal to the button state
buttonState = digitalRead(buttonPin);

//Set up an IF statement which determines if the button is pressed.
if (buttonState == HIGH) {
//Set up a for loop which loops a hundred times
for (int i = 0; i > 100; i++) {
//First digitally write the LED pin as high, turning it on
digitalWrite(ledPin, HIGH);
//Then delay for whatever the counter, i, is equal to
delay(i);
//Then digitally write the LED pin as low, turning it off
digitalWrite(ledPin, LOW);
//Then delay for whatever the counter, i, is equal to
delay(i);
}
}

// If the button is not pressed, set the LED to low (off).
else {
digitalWrite(ledPin, LOW);
}
}

Analog Pin

int ledPinOne = 10; //setting up variables for all three LEDs
int ledPinTwo = 6;
int ledPinThree = 2;

const int analogPin = 0; //for the flex sensor
const int flex = 100; //value

void setup() {

pinMode(ledPinOne, OUTPUT);//setting up all three LEDs to be outputs
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);

Serial.begin(9600);
}

void loop()
{

int analogValue = analogRead(analogPin);

if (analogValue > flex) { //if when reading the flex it becomes larger then the value of 100, then //do this
digitalWrite(ledPinOne, HIGH); //light it up
delay(100); //wait 100 millaseconds
}

else { //or if else turn it off and wait 100 millaseconds, and same for the other two LEDs
digitalWrite(ledPinOne,LOW);
delay(100);
}

if (analogValue > flex) {
digitalWrite(ledPinTwo, HIGH);
delay(200);
}
else {
digitalWrite(ledPinTwo,LOW);
delay(200);
}

if (analogValue > flex) {
digitalWrite(ledPinThree, HIGH);
delay(400);
}
else {
digitalWrite(ledPinThree,LOW);
delay(400);
}



Serial.println(analogValue, DEC); //print it


}

Tuesday, October 6, 2009

Using Analog Reading + Implemented pseudo code - Tsveta

const int ledPin = 13;//declare pin 13
const int buttonPin = 2;// declare pin 2
int sensorPin = 4;// declare pin 4

void setup() {
pinMode(buttonPin, INPUT);//set pin 2 to be an input source
pinMode(sensorPin, INPUT);// set pin 4 to be an input source
pinMode(ledPin, OUTPUT);// set pin 13 to be an output source
}

void loop() {
int getstate = digitalRead(buttonPin); //read the state of the button pin and
//inputs the value into the variable getstate
int timereceived = analogRead(sensorPin);//reads analog data
if (getstate == HIGH){//if button is pressed
digitalWrite(ledPin, HIGH);//turn the LED on
delay(timereceived);//assigns the data read from the sensor input to the delay time
}
delay(timereceived / 100);//delay to remove noise
if (getstate == LOW){//if button is released
digitalWrite(ledPin, LOW);//turn the LED off
delay(timereceived);//assigns the data read from the sensor input to the delay time
}
}

Joe Cohen | 8/7/09 | Parts 1, 2 & 3

#1


*In this setup, the microcontroller reads light via photocell. This light is converted into a number which is used as part of a value to determine the length of time the LED stays lit. The less amount of light, the less amount of time the LED stays lit (often does not light at all, especially with those fat zero values). The more amount of light, the brighter the LED will be.

/* Code by Wm. Joe Cohen for
Physical Computing Studio
Due 10/6/09 | Part I */

int ledPinA = 3;
int buttonPin = 8;
int buttonState = 0;
int lastButtonState = LOW;
int photoPin = 0;
int photoValue = 0;

long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {

pinMode(ledPinA, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(photoPin, INPUT);
Serial.begin(9600);
}

void loop() {
int lightTime = photoValue * 100;
int reading = digitalRead(buttonPin);
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) {
photoValue = analogRead(photoPin);
Serial.println(photoValue);
}
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}
digitalWrite(ledPinA, buttonState);
delay(lightTime);
digitalWrite(ledPinA, LOW);
lastButtonState = reading;
}

I have set values to my variables for an LED, button, and photocell. Debounce time and delay is initialized as long. In the setup, the LED pin is set to output, the button pin is set to input, the photocell pin is set to input, and a serial print reading is established. Now for the loop. A variable 'lightTime' is created to hold the values that are being input from the photocell x 100. The state of the button is read. When the button is pressed, the values from the photocell are read and printed in the serial monitor. A debounce is thrown in to prevent double takes of input. The light turns on from the fact that the button is pressed. The high values keep the LED lit for longer and the low values allow for a short amount of time or none at all.
__________________________________________________

#2


/* Code by Wm. Joe Cohen for
Physical Computing Studio
Due 10/6/09 | Part II */

int ledPinA = 2;
int ledPinB = 3;
int ledPinC = 4;
int buttonPin = 8;
int buttonState = 0;


void setup() {

pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) {

for (int x = 0; x<100;>
//lights go one way
digitalWrite(ledPinA, HIGH);
delay(x);
digitalWrite(ledPinA, LOW);
delay(x);
digitalWrite(ledPinB, HIGH);
delay(x);
digitalWrite(ledPinB, LOW);
delay(x);
digitalWrite(ledPinC, HIGH);
delay(x);
//lights go the other way
digitalWrite(ledPinC, LOW);
delay(x);
digitalWrite(ledPinB, HIGH);
delay(x);
digitalWrite(ledPinB, LOW);
delay(x);
digitalWrite(ledPinA, HIGH);
delay(x);
digitalWrite(ledPinA, LOW);
delay(x);
}
}
delay(1);
}

I have set values to my variables for 3 LEDs on separate digital pins, as well as a button pin. In the setup, all the LEDs are set to output and the button is used for input. Loop time! The state of the button is read. If the button is pressed, all 3 LEDs light up with delays according to an incrimental variable 'x'. The lights bounce back and forth, as they get slower and slower. The loop is delayed for 1 millisecond and waits for the next time the button is pressed.
__________________________________________________

#3



/* Code by Wm. Joe Cohen for
Physical Computing Studio
Due 10/6/09 | Part III */

int ledPin = 3;
int buttonPin = 8;
int buttonState = 0;
int lastButtonState = LOW;

long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {

pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
int reading = digitalRead(buttonPin);
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) {
for(int x = 0; x<100;>
digitalWrite(ledPin, HIGH);
delay(x/2);
digitalWrite(ledPin, LOW);
delay(x*2);
digitalWrite(ledPin, HIGH);
delay(x);
digitalWrite(ledPin, LOW);
delay(x);
}
}
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}
digitalWrite(ledPin, buttonState);
lastButtonState = reading;
}

I have set values to my variables for 1 LED, a button, and debounce longs. In the setup, the LED is set as output and the button is set as input. The loop begins. The state of the button is read. If the button is pressed, the LED starts to pulse according to delays set to an incremental variable called 'x' along with minor alterations in the first two delays (division and multiplication). The code is debounced.