Arduino Robotics Assignment 3

In today’s class, the first half of the class focused on arrays and functions , and the second half of the class Michael taught us how to do soldering.




I found there are so many similarities with JavaScript: the concepts of arrays, functions. But there are also some differences, such as to create a function, in Arduino, there is no “function” word ahead. Personally, I prefer the JavaScript way of creating a function, because it is more clear that it is a function.




Connect 3 LEDs to three non-adjacent pins (e.g. 3, 12, 7) on your Arduino. Make the LEDs blink in turn like you did in step #6 yesterday. Use for() loops and an array.
The schematics

About the code:

creating a new array can be outside the loop and outside setup. But to define the elements in the loop in each line needs to be within the loop. And there is a more simple way below.

int foo[3];//foo is the name of the array
//there are 3 elements in the array
void setup() {
}
void loop() {
foo[0]=3;//the first pin is #3
foo[1]=5;//the second pin is #5
foo[2]=8;//the third pin is #8
for(int i=0; i< 3; i++ ) {
pinMode(foo[i], OUTPUT);
digitalWrite(foo[i], HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(foo[i], LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}

int foo[3]={3,5,8};//create an array which has three elements:3,5,8
void setup() {
}
void loop() {
for(int i=0; i< 3; i++ ) {
pinMode(foo[i], OUTPUT);
digitalWrite(foo[i], HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(foo[i], LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
Write a function that turns off the three LEDs in problem #1, and modify your solution to problem #1 to use this function. In the loop, use the function to turn off all LEDs, then turn on only the one LED that should be on.

First Trial — -failed, the leds don’t turn off

The code:

int foo[3] ={3,5,8};
void turnOff(int i){  //the turnOff function
pinMode(foo[i],OUTPUT);
digitalWrite(foo[i], LOW);
}
void turnOn(int j){ //the turnOn function
pinMode(foo[j],OUTPUT);
digitalWrite(foo[j], HIGH);
}
void setup() {
}
void loop() {
for(int h=0; h< 3; h++ ) {
turnOn(h);
turnOff(h+1);
turnOff(h+2);
delay(1000);
}
}

Second Trial — — fail — — -all the leds are not turning on

int foo[3] ={3,5,8};

void turnOn(int j){
pinMode(foo[j],OUTPUT);
digitalWrite(foo[j], HIGH);
}
void turnOffAll(){
pinMode(3,OUTPUT);
digitalWrite(3,LOW);
pinMode(5,OUTPUT);
digitalWrite(5,LOW);
pinMode(8,OUTPUT);
digitalWrite(8,LOW);
}
void setup() {
}
void loop() {
for(int h=0; h< 3; h++ ) {
turnOn(h);
turnOffAll();
delay(1000);
}
}

Third trial — -succeed


int foo[3] ={3,5,8};

void turnOn(int j){
pinMode(foo[j],OUTPUT);
digitalWrite(foo[j], HIGH);
}
void turnOffAll(){
pinMode(3,OUTPUT);
digitalWrite(3,LOW);
pinMode(5,OUTPUT);
digitalWrite(5,LOW);
pinMode(8,OUTPUT);
digitalWrite(8,LOW);
}
void setup() {
}
void loop() {
for(int h=0; h< 3; h++ ) {
turnOn(h);
delay(1000);
turnOffAll();

}
}

A better version — — a loop within the turnOffAll() function.

int foo[3] ={3,5,8};

void turnOn(int j){
pinMode(foo[j],OUTPUT);
digitalWrite(foo[j], HIGH);
}
void turnOffAll(){
for(int g=0;g<3;g+=1){
pinMode(foo[g],OUTPUT);
digitalWrite(foo[g],LOW);
}
}
void setup() {
}
void loop() {
for(int h=0; h< 3; h++ ) {
turnOn(h);
delay(1000);
turnOffAll();

}
}