For the “stupid pet trick” assignment, I made a Pacman that “ate” the LEDs coming towards it.
The back is somewhat of a mess, despite my efforts to tidy up the wires as much as possible, just because it was coming out of all over everywhere. I used maximum number of LEDs, which is as many output pins as I had. The speed of the LEDs moving towards the Pacman and the movement of the Pacman can be altered by the potentiometer.
#include <Servo.h> Servo myservo; int pos = 90; int val; int sensorPin = 0; int startLED = 2; int endLED = 7; void setup() { // put your setup code here, to run once: for(int i = startLED; i < endLED + 1; i++){ pinMode(i, OUTPUT); } myservo.attach(9); myservo.write(pos); } void loop() { // put your main code here, to run repeatedly: int sensorValue = analogRead(sensorPin); for (int i = startLED; i < endLED + 1; i++){ digitalWrite(i, HIGH); sensorValue = analogRead(sensorPin); delay(sensorValue); digitalWrite(i, LOW); sensorValue = analogRead(sensorPin); delay(sensorValue); } myservo.write(pos - 45); delay(sensorValue); myservo.write(pos); }
Initially I found that without having to do analogRead before every time I did delay, the effect of turning the potentiometer would also be delayed. As a result, putting analogRead before doing a delay made the Pacman more responsive to the changes of the potentiometer.