/* pin assignments: 9 - R1 10 - R2 11 - R3 12 - R4 */ int speedValue; int R1 = 9; int R2 = 10; int R3 = 11; int R4 = 12; void setup(){ pinMode(R1, OUTPUT); pinMode(R2, OUTPUT); pinMode(R3, OUTPUT); pinMode(R4, OUTPUT); off(); delay(1000); } void loop(){ forward(); delay(3000); reverse(); delay(3000); off(); delay(3000); } void forward() { digitalWrite(R1, LOW); digitalWrite(R2, LOW); digitalWrite(R4, HIGH); // ramp in from min to max in increments of 5 points: for(int speedValue = 0 ; speedValue <= 255; speedValue +=5) { // sets the value (range from 0 to 255): analogWrite(R3, speedValue); // wait for 30 milliseconds to see the effect delay(30); } // ramp out from max to min in increments of 5 points: for(int speedValue = 255 ; speedValue >= 0; speedValue -=5) { // sets the value (range from 0 to 255): analogWrite(R3, speedValue); // wait for 30 milliseconds to see the effect delay(30); } } void reverse(){ digitalWrite(R2, HIGH); digitalWrite(R3, LOW); digitalWrite(R4, LOW); // ramp in from min to max in increments of 5 points: for(int speedValue = 0 ; speedValue <= 255; speedValue +=5) { // sets the value (range from 0 to 255): analogWrite(R1, speedValue); // wait for 30 milliseconds to see the effect delay(30); } // ramp out from max to min in increments of 5 points: for(int speedValue = 255 ; speedValue >= 0; speedValue -=5) { // sets the value (range from 0 to 255): analogWrite(R1, speedValue); // wait for 30 milliseconds to see the effect delay(30); } } void off() { digitalWrite(R1, LOW); digitalWrite(R2, HIGH); digitalWrite(R3, LOW); digitalWrite(R4, HIGH); }