Lab Sensing Part 1: Pots

Posted by jlzych

jlzych's picture

I programmed the microcontroller to control the 3 LEDs with 3 potentiometers. The hex value the LEDs are displaying is printed to Serial.

 

Code

 
/*
* 3 pots fade RGB LEDs
 * modified version of AnalogInput
 * by DojoDave <http://www.0j0.org>
 * http://www.arduino.cc/en/Tutorial/AnalogInput 
 */

// Offset of the LED pins from the pot inputs (0-2)
// Ex. first LED is 0 + 9 = 9, pins 9-11 are the LEDs
int ledOffset = 9;

// Stores the RGB values
int vals[3] = {0, 0, 0};

// Stores value being read
int val = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // Loops from 0 - 2, assumes pot inputs correspond to this
  for (int i = 0; i < 3; i++) {
    val = analogRead(i)/4;
    if (val != vals[i] &&
      val != vals[i] + 1) {
      analogWrite(i + ledOffset, val);
      vals[i] = val;
      
      char s[50];
      sprintf(s, "Hex value: #%02x%02x%02x", vals[0], vals[1], vals[2]);
      Serial.println(s);
    }
  }
  delay(100);
}
Second part - 3 pots
First part - one pot
0
Your rating: None
Tags: