0% found this document useful (0 votes)
6 views

Arduino RGB 7 Segment Setup

Displays Unit on 7 Segment based on button pressed

Uploaded by

lollipopness123
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Arduino RGB 7 Segment Setup

Displays Unit on 7 Segment based on button pressed

Uploaded by

lollipopness123
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Arduino.

h>
#include <avr/io.h>
#include <util/delay.h>

// Pin definitions
#define BUTTON1_PIN 2 // Button1 pin
#define BUTTON2_PIN 3 // Button2 pin
#define BUTTON0_PIN 4 // Button0 pin
#define ADC_PIN A0 // ADC pin for photocell

#define RGB_RED_PIN 5 // Red pin of RGB LED


#define RGB_GREEN_PIN 6 // Green pin of RGB LED
#define RGB_BLUE_PIN 9 // Blue pin of RGB LED

// Global variables
volatile uint8_t adcValue = 0; // Variable to store ADC value
volatile uint8_t redDutyCycle = 0; // Duty cycle for Red LED (0-255)
volatile uint8_t greenDutyCycle = 0;// Duty cycle for Green LED (0-255)
volatile uint8_t blueDutyCycle = 0; // Duty cycle for Blue LED (0-255)

// Function declarations
void setup();
void loop();
void initializeInterrupts();
void readADC();
void updateLEDs();
void button1ISR();
void button2ISR();
void button0ISR();

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

// Pin configurations
pinMode(RGB_RED_PIN, OUTPUT);
pinMode(RGB_GREEN_PIN, OUTPUT);
pinMode(RGB_BLUE_PIN, OUTPUT);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON0_PIN, INPUT_PULLUP);

initializeInterrupts();
}

void loop() {
readADC(); // Read ADC value
updateLEDs(); // Update LED PWM based on ADC value
Serial.print("ADC Value: ");
Serial.print(adcValue);
Serial.print(" Red Duty Cycle: ");
Serial.print(redDutyCycle);
Serial.print(" Green Duty Cycle: ");
Serial.print(greenDutyCycle);
Serial.print(" Blue Duty Cycle: ");
Serial.println(blueDutyCycle);
delay(1000);
}

void initializeInterrupts() {
// Setup pin change interrupts for Button1 and Button2
attachInterrupt(digitalPinToInterrupt(BUTTON1_PIN), button1ISR, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON2_PIN), button2ISR, FALLING);

// Setup INT0 interrupt for Button0


attachInterrupt(digitalPinToInterrupt(BUTTON0_PIN), button0ISR, FALLING);
}

void readADC() {
adcValue = analogRead(ADC_PIN) / 4; // Convert 10-bit ADC value to 8-bit (0-
255)
}

void updateLEDs() {
// Map ADC value to duty cycles for RGB LED
redDutyCycle = adcValue;

analogWrite(RGB_RED_PIN, redDutyCycle);
analogWrite(RGB_GREEN_PIN, greenDutyCycle);
analogWrite(RGB_BLUE_PIN, blueDutyCycle);
}

void button1ISR() {
// Increment green duty cycle by 5
greenDutyCycle += 5;
if (greenDutyCycle > 255) greenDutyCycle = 255; // Clamp at 255
}

void button2ISR() {
// Increment blue duty cycle by 5
blueDutyCycle += 5;
if (blueDutyCycle > 255) blueDutyCycle = 255; // Clamp at 255
}

void button0ISR() {
// Interrupted by Button0 falling edge
// Prompt user to enter a three-digit number
Serial.println("Please enter a three-digit number:");
while (Serial.available() < 3) {} // Wait until three digits are entered
int number = 0;
for (int i = 0; i < 3; i++) {
number *= 10;
number += (Serial.read() - '0'); // Convert ASCII to integer
}
// Display each digit for one second on 7-Segment
for (int i = 2; i >= 0; i--) {
Serial.println(number / pow(10, i));
delay(1000);
number %= (int)pow(10, i);
}
}

You might also like