Monthly Archives: April 2014

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

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);
}