Having fun with LED rings and Keypad

These rings was ment for my RepRap 3D printer, but until I got it assembled and working, I could have a bit fun with the rings first.

To make it work, I am using this code. It will fade the ring up and down in different speeds, and also allow for manual control of the light.

//Fade a led without delaying the rest of the code
//START
unsigned long fadeMillis = 0;
boolean fadeUp = true;
int fadeValue = 0;
int fDelay = 5;
boolean fade = true;
#define fled 11

void analogFade()
{
  if (!fade) return;

  if (millis() - fadeMillis >= fDelay)
  {
    fadeMillis = millis();

    if (fadeUp)
    {
      if (fadeValue < 255)
      {
        fadeValue++;
      }
      else
      {
        fadeUp = false;
        fadeValue--;
      }
    }
    else
    {
      if (fadeValue > 0)
      {
        fadeValue--;
      }
      else
      {
        fadeUp = true;
        fadeValue++;
      }
    }
    analogWrite(fled, fadeValue);
  }
}
//STOP

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {
    '1','2','3','A'  }
  ,
  {
    '4','5','6','B'  }
  ,
  {
    '7','8','9','C'  }
  ,
  {
    '*','0','#','D'  }
};

byte colPins[COLS] = {
  2, 3, 4, 5}; //connect to the column pinouts of the keypad
byte rowPins[ROWS] = {
  6, 7, 8, 9}; //connect to the row pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup()
{
  Serial.begin(9600);
  pinMode(fled, OUTPUT);
}

void loop()
{
  char customKey = cusomKeypad.getKey();

  if (customKey != NO_KEY)
  {
    switch (customKey)
    {
    case '0':
      fDelay = 0;
      fade = true;
      break;
    case '1':
      fDelay = 1;
      fade = true;
      break;
    case '2':
      fDelay = 2;
      fade = true;
      break;
    case '3':
      fDelay = 3;
      fade = true;
      break;
    case '4':
      fDelay = 4;
      fade = true;
      break;
    case '5':
      fDelay = 5;
      fade = true;
      break;
    case '6':
      fDelay = 6;
      fade = true;
      break;
    case '7':
      fDelay = 7;
      fade = true;
      break;
    case '8':
      fDelay = 8;
      fade = true;
      break;
    case '9':
      fDelay = 9;
      fade = true;
      break;
    case 'A':
      fDelay++;
      fade = true;
      break;
    case 'B':
      fDelay--;
      fade = true;
      if (fDelay < 0) fDelay = 0;
      break;
    case 'C':
      fadeValue = fadeValue + 5;
      fade = false;
      if (fadeValue > 255) fadeValue = 255;
      analogWrite(fled, fadeValue);
      break;
    case 'D':
      fadeValue = fadeValue - 5;
      fade = false;
      if (fadeValue < 0) fadeValue = 0;
      analogWrite(fled, fadeValue);
      break;
    default:
      Serial.println(customKey);
    }
  }

  analogFade();
}

and after I was done playing around, I just had to try to add more rings…

The rings are connected to an ULN2003A, and then to the arduino’s pin 11 for PWM.

Leave a Reply