Skip to content
Snippets Groups Projects
Commit bd9bedcc authored by Jacob Kieser's avatar Jacob Kieser
Browse files

Project code

parent ef218449
No related branches found
No related tags found
No related merge requests found
// Define constants
const bool COMMON_ANODE = false;
const int RGB_RED_PIN = 6;
const int RGB_GREEN_PIN = 5;
const int RGB_BLUE_PIN = 3;
const int DELAY_MS = 20;
const int MAX_COLOR_VALUE = 255;
const int PHOTOCELL_INPUT_PIN = A0;
const int MIN_PHOTOCELL_VAL = 200;
const int BUTTON_PIN = 2;
const int GRAPHITE_INPUT = A1;
const int CREATIVE_FEATURE = A3;
const int MAX_PHOTOCELL_VAL = 800;
const bool PHOTOCELL_IS_R2_IN_VOLTAGE_DIVIDER = true;
const int FADE_STEP = 5;
const int BLINK_INTERVAL = 500;
const int SOLO_RED_RGB = 7;
// Define enumerations
enum RGB { RED, GREEN, BLUE, NUM_COLORS };
// Global variables
int mode = 0;
int rgbLedValues[] = {MAX_COLOR_VALUE, 0, 0};
RGB curFadingUpColor = GREEN;
RGB curFadingDownColor = RED;
unsigned long lastBlinkTime = 0;
void setup() {
// Set pin modes
pinMode(RGB_RED_PIN, OUTPUT);
pinMode(RGB_GREEN_PIN, OUTPUT);
pinMode(RGB_BLUE_PIN, OUTPUT);
pinMode(PHOTOCELL_INPUT_PIN, INPUT);
pinMode(GRAPHITE_INPUT, INPUT);
pinMode(SOLO_RED_RGB, OUTPUT);
pinMode(CREATIVE_FEATURE, INPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize serial communication
Serial.begin(9600);
// Set initial color
setColor(rgbLedValues[RED], rgbLedValues[GREEN], rgbLedValues[BLUE], 0);
delay(DELAY_MS);
}
void loop() {
unsigned long currentTime = millis();
// Read the button state
int buttonState = digitalRead(BUTTON_PIN);
// Check if the button is pressed
if (buttonState == LOW) {
// Cycle through the modes
mode = (mode + 1) % 3;
// Add a delay to debounce the button
delay(250);
}
// Implement different behavior based on the mode
switch (mode) {
case 0:
faderMode(currentTime);
break;
case 1:
blinkMode(currentTime);
break;
case 2:
brightnessMode();
break;
}
delay(DELAY_MS);
}
// RGB Fading largely based off the Interactive Textbook
void faderMode(unsigned long currentTime) {
int photocellVal = analogRead(PHOTOCELL_INPUT_PIN);
int ledVal = map(photocellVal, MIN_PHOTOCELL_VAL, MAX_PHOTOCELL_VAL, 0, MAX_COLOR_VALUE);
ledVal = constrain(ledVal, 0, MAX_COLOR_VALUE);
if (!PHOTOCELL_IS_R2_IN_VOLTAGE_DIVIDER) {
ledVal = MAX_COLOR_VALUE - ledVal;
}
rgbLedValues[curFadingUpColor] += FADE_STEP;
rgbLedValues[curFadingDownColor] -= FADE_STEP;
if (rgbLedValues[curFadingUpColor] > MAX_COLOR_VALUE) {
rgbLedValues[curFadingUpColor] = MAX_COLOR_VALUE;
curFadingUpColor = static_cast<RGB>((curFadingUpColor + 1) % NUM_COLORS);
}
if (rgbLedValues[curFadingDownColor] < 0) {
rgbLedValues[curFadingDownColor] = 0;
curFadingDownColor = static_cast<RGB>((curFadingDownColor + 1) % NUM_COLORS);
}
setColor(rgbLedValues[RED], rgbLedValues[GREEN], rgbLedValues[BLUE], ledVal);
}
void blinkMode(unsigned long currentTime) {
int circuitState = analogRead(CREATIVE_FEATURE);
if (currentTime - lastBlinkTime >= BLINK_INTERVAL && circuitState < 50) {
rgbLedValues[RED] = (rgbLedValues[RED] == 0) ? MAX_COLOR_VALUE : 0;
rgbLedValues[GREEN] = 0;
rgbLedValues[BLUE] = 0;
lastBlinkTime = currentTime;
}
setColor(rgbLedValues[RED], rgbLedValues[GREEN], rgbLedValues[BLUE], MAX_COLOR_VALUE);
}
void brightnessMode() {
int resistorValue = analogRead(GRAPHITE_INPUT);
int ledBrightness = map(resistorValue, 0, 1023, 0, MAX_COLOR_VALUE);
ledBrightness = constrain(ledBrightness, 0, MAX_COLOR_VALUE);
setColor(100, 100, 100, ledBrightness);
}
// Sets color for the RGB LED - Largely based off the Interactive Textbook
void setColor(int red, int green, int blue, int ledVal) {
if (COMMON_ANODE) {
red = MAX_COLOR_VALUE - red;
green = MAX_COLOR_VALUE - green;
blue = MAX_COLOR_VALUE - blue;
}
analogWrite(SOLO_RED_RGB, red);
analogWrite(RGB_RED_PIN, min(red, ledVal));
analogWrite(RGB_GREEN_PIN, min(green, ledVal));
analogWrite(RGB_BLUE_PIN, min(blue, ledVal));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment