1x Windows Computer with Visual Studio
1x Arduino UNO
1x RGB Led
3x 330 ohm resistors (I needed to use 2 more to calibrate the RGB led, Green and Blue where to bright compared to Red)
1x Breadboard
Some jumpwires
Result:
How did i do it?
C# Code:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
namespace RGBLedColorPicker
{
    public partial class Form1 : Form
    {
        // Initialize serial port
        SerialPort port = new SerialPort("COM3", 9600);
        // Initialize the default color to black
        private Color defaultColor = Color.FromArgb(0, 0, 0);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Open connection to Arduino
            port.Open();
            // Set default color
            this.SetColor(defaultColor);
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Set default color
            this.SetColor(defaultColor);
            
            // Close connection to Arduino
            port.Close();
        }
        private void panel1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                SetColor(colorDialog1.Color);
            }
        }
        private void SetColor(Color color)
        {
            // Update color in the panel
            panel1.BackColor = color;
            // Write color to Arduino
            port.Write(new[] { color.R, color.G, color.B }, 0, 3);
        }
    }
}
Arduino Code:
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
void setup() { 
  Serial.begin(9600);
}
void loop() {
  if (Serial.available() == 3)
  {
    analogWrite(RED_LED_PIN, Serial.read());
    analogWrite(GREEN_LED_PIN, Serial.read());
    analogWrite(BLUE_LED_PIN, Serial.read());
  }
}
No comments:
Post a Comment