Activity 4: Controlling Digital Output Using Analog Input

Estimated completion time:

30 - 40 minutes

Electronic Systems: The Robot & The Codes (Page 74 &75)


Link to tinkerCAD Model

Please note that the pin numbers for the given code is different to the ones in tinkerCAD.

The tinkerCAD model uses pin A0, A1, A2, A3, and A4.


https://www.tinkercad.com/things/dzndOLYyZZS

Exercise Hint #1

Repeat instruction in every section (before void setup, in void setup, and in void loop) for every component (IR1, IR2, LED2, and LED3).


Similar to activity 1

Exercise Hint #2

Comment out or remove "Serial.begin(9600);"

Example:

//Serial.begin(9600);

Exercise Hint #3

Use two separate If Else statement for each OUTPUT component.


Example:


void loop () {

int state1= analogRead(ir1), state3= analogRead(ir3);


if (state1<state1_white){

digitalWrite(L1,HIGH);

}


else if (state1>state1_white){

digitalWrite(L1,LOW);

}


if (state3<state3_white){

digitalWrite(L3,HIGH);

}


else if (state3>state3_white){

digitalWrite(L3,LOW);

}

}


(P/S: This is not a complete code)