Wednesday, November 21, 2012

RGB Led Color Picker using C# and an Arduino

Used:
 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());
  }
}

Thursday, November 15, 2012

WCF Service in a console app

Create a new console app:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Add Reference.... Select System.ServiceModel from the .NET tab and click OK.
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCFConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Service1));
            host.Open();
            Console.Write("Service is up and running");
            Console.ReadKey();
            host.Close();
        }
    }

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GreetingMessage(string Name);
    }

    public class Service1 : IService1
    {
        public string GreetingMessage(string name)
        {
            return "Welcome to WCF " + name;
        }
    }
}

Add App.config:

  
    
      
        
        
          
          
            
          
        
        
        
          
            
          
        
      
    
    
      
        
          
          
          
          
        
      
    
  

soapUI Compatible App.config:
Remember to activate WS-A options in your request:
Add default wsa:Action
Add default wsa:To