Fun Cylon eyeball project

For this project you will need 6 led’s, 6 22 Ohm resistor’s, one potentiometer, one push button, and one arduino. I am using an uno in my sketch. I will put my code down below.

// Harrison's Fun Cylon eyeball project

#define PIN_COUNT 6
int UPDATE_DURATION = 5;

// Uno (those with the ATmega168 or ATmega328)
// PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function
// On the Arduino Mega, it works on pins 2 to 13 and 44 to 46; Provide 8-bit PWM output with the analogWrite() function.
//
int pins[PIN_COUNT] = { 3, 5, 6, 9, 10, 11};
int states[PIN_COUNT];
int current_pin = 0;
int dir = 1;
int update_count = 0;

void setup() {
  for ( int i = 0; i < PIN_COUNT; i++ ) {
    pinMode(pins[i], OUTPUT);
    states[i] = 0;
     pinMode(4, INPUT_PULLUP);
  }
}

void updatePins() {
  for ( int i = 0; i < PIN_COUNT; i++ ) {
    analogWrite(pins[i], states[i]); // AnalogWrite uses pulse width modulation (PWM), 
     //analogWrite(pins[i], states[i] * 4);                                // turning a digital pin on and off very quickly, 
                                     // to create a fading effect. 
  }
  delay(5);
}

void decay() {
  for ( int i = 0; i < PIN_COUNT; i++ ) {
    states[i] = (19*states[i]/20);
    //states[i] = (2*states[i]/600);
  }
}

void loop() {
  int sensorVal = digitalRead(4);

   if (sensorVal != HIGH) {
    
     for ( int i = 0; i < PIN_COUNT; i++ ) {
    analogWrite(pins[i], 0); // AnalogWrite uses pulse width modulation (PWM), 
     //analogWrite(pins[i], states[i] * 4);                                // turning a digital pin on and off very quickly, 
                                     // to create a fading effect. 
  }
    return;
   } //else {

  //int UPDATE_DURATION = analogRead(A0);
int UPDATE_DURATION = analogRead(A0);
  UPDATE_DURATION = map(UPDATE_DURATION, 0, 1023, 5  , 100);
  analogWrite(2, UPDATE_DURATION);

  decay();
  states[current_pin] = 255 * update_count / UPDATE_DURATION;
  updatePins();
  
  update_count++;
  if ( update_count > UPDATE_DURATION ) {
    update_count = 0  ;
    current_pin += dir;
    if ( current_pin == 0 ) {
      dir = 1;
    }
    else if ( current_pin == (PIN_COUNT-1) ) {
      dir = -1;
    }
  }
   
}  

Some problems I had at first When using the
potentiometer at first it is super sensitive that is why I use PDATE_DURATION = map(UPDATE_DURATION, 0, 1023, 5 , 100);

next the button only paused on the first try and so I put

if (sensorVal != HIGH) {

 for ( int i = 0; i < PIN_COUNT; i++ ) {
analogWrite(pins[i], 0); // AnalogWrite uses pulse width modulation (PWM),  

to turn the led’s full off.

Leave a Reply