Week4 : Make my own switches

I tried my switches ‘on’ and ‘off’. It was little tough one than the last week’s work.

I was confused at the first time, but after some failures, I found my mistakes and I went through it finally..

I think Arduino is getting more complicated..^^;

 

 

1. Serial Output

code>>

 

int switchArray[] = {2, 3};
int stateArray[] = {0, 0};
int prevArray[] = {0, 0};
int numSwitches = 2;
int counter;
int myDelay = 50;
void setup() {

for(counter = 0; counter < 2; counter++)
{
pinMode(switchArray[counter], INPUT);
}
Serial.begin(9600);

}
void loop() {
for(counter = 0; counter < numSwitches; counter++)
{
stateArray[counter] = digitalRead(switchArray[counter]);
if(stateArray[counter] != prevArray[counter]){
if(stateArray[counter] == 1){
Serial.print(“switch”);
Serial.print(counter);
Serial.println();
}
prevArray[counter] = stateArray[counter];
}
}
delay(myDelay);
}

 

2. Switches

 

int switchArray[] = {2, 3};
int stateArray[] = {0, 0};
int prevArray[] = {0, 0};
int ledArray[] = {4, 5};
int numSwitches = 2;
int counter;
int myDelay = 50;

void setup() {

for(counter= 0;counter < 2; counter++)
{
pinMode(switchArray[counter], INPUT);
pinMode(ledArray[counter], OUTPUT);

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

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

stateArray[counter] = digitalRead(switchArray[counter]);
if(stateArray[counter] != prevArray[counter]){
if(stateArray[counter] == 1){
digitalWrite(ledArray[counter], HIGH);
Serial.print(“switch”);
Serial.print(counter);
Serial.println();

}
else{
digitalWrite(ledArray[counter], LOW);
}
prevArray[counter] = stateArray[counter];
}
}

delay(myDelay);
}

 

 

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