Categories

RGB Color picker and C sharp

This is just another simple app to control an RGB led, this time the app is a color picker

Schematic

Arduino RGB schematic

Arduino RGB schematic

Arduino Code

const int RED_LED = 9;
const int GREEN_LED = 10;
const int BLUE_LED = 11;
 
void setup() 
{ 
  Serial.begin(9600);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);
  analogWrite(RED_LED, 255);
  analogWrite(GREEN_LED, 255);
  analogWrite(BLUE_LED, 255);
}
 
void loop() 
{
  if (Serial.available() == 3)
  {
    analogWrite(RED_LED, 255 - Serial.read());
    analogWrite(GREEN_LED, 255 - Serial.read());
    analogWrite(BLUE_LED, 255 - Serial.read());
  }
}

Now we will build our C# app, create a new winforms project and a panel from the toolbox, also add a serialport and colordialog control.

rgb color picker

Now when you click on the panel a Color dialog box will appear, selecting a color will send those color details to the Arduino which will try and display the appropriate color, this is not perfect so some of the more unusual colors will not be an exact match.

Here is a picture of this

rgb color picker 1

 

C# Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
 
namespace RGBPicker
{
    public partial class Form1 : Form
    {
        SerialPort port = new SerialPort("COM5", 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)
        {
            port.Open();
            this.SetRGBColor(defaultColor);
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            port.Close();
        }
 
        private void panel1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                SetRGBColor(colorDialog1.Color);
            }
        }
 
        private void SetRGBColor(Color color)
        {
            // Update color
            panel1.BackColor = color;
            // send color to Arduino
            port.Write(new[] { color.R, color.G, color.B }, 0, 3);
        }
    }
}

 

Link

https://github.com/arduinolearning/Interfacing/tree/master/RGB%20Color%20Picker

Share

Comments are closed.