Week5 : Analog Input&Output

Soldering iron was very interesting experience.

I was scared at first time, but when I finished my own soldering work, I felt so proud of myself.

 

 

1. Analog Input Serial

 

code>>
int analogIn0 = A0;

int analogValue0;

int myDelay = 5;
void setup()
{

Serial.begin(9600);
}
void loop()
{

analogValue0 = analogRead(analogIn0);

Serial.print(“analogValue0 = ” );
Serial.println(analogValue0);

delay(myDelay);
}

2. Analog Input Output

 

code>>
int led0 = 3;

int analogIn0 = A0;

int analogValue0;

int myDelay = 5;
void setup()
{

Serial.begin(9600);
}
void loop()
{

analogValue0 = analogRead(analogIn0);

analogValue0 = map(analogValue0, 0, 1023, 0, 255);

analogWrite(led0, analogValue0);

Serial.print(“analogValue0 = ” );
Serial.println(analogValue0);

delay(myDelay);
}

 

3. Digital Input Interesting Output

 

code>>
int switchArray[] = {
2, 3, 4};
int stateArray[] = {
0, 0, 0};
int prevArray[] = {
0, 0, 0};
int ledArray[] = {
5, 6, 7};

int numSwitches = 3;
int count;
int blinkTime = 50;
int myDelay = 50;
void setup() {

for(count = 0;count < numSwitches; count++)
{

pinMode(switchArray[count], INPUT);

pinMode(ledArray[count], OUTPUT);

}

Serial.begin(9600);
}
void loop() {

for(count = 0; count < numSwitches; count++)
{

stateArray[count] = digitalRead(switchArray[count]);

if(stateArray[count] != prevArray[count]){

if(stateArray[count] == 1){

switch(count){
case 0:
lefttoright();
break;
case 1:
righttoleft();
break;
case 2:
randomLeds();
break;
}

Serial.print(“switch”);
Serial.print(count);
Serial.println();
}
else{

digitalWrite(ledArray[count], LOW);
}

prevArray[count] = stateArray[count];
}
}

delay(myDelay);
}

 

void lefttoright(){

for(count = 0;count <= numSwitches; count++)
{
digitalWrite(ledArray[count], HIGH);
delay(blinkTime);
digitalWrite(ledArray[count], LOW);
delay(blinkTime);
}
}

void righttoleft(){
for(count = numSwitches;count >= 0; count–)
{
digitalWrite(ledArray[count], HIGH);
delay(blinkTime);
digitalWrite(ledArray[count], LOW);
delay(blinkTime);
}
}

void randomLeds(){
for(count = 0;count <= 10; count++)
{
digitalWrite(ledArray[random(4)], HIGH);
delay(blinkTime);
digitalWrite(ledArray[random(4)], LOW);
delay(blinkTime);
}
}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s