Categories

keyestudio Multi-purpose shield V2 – flashing led example

The  keyestudio Multi-purpose shield V2 has 6 leds, in this example we will flash the LEDs

The LEDs are connected to pins D8 to D13

Code

//pin definition of LEDs
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 8;
 
void setup ()
{
 
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT); 
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);
  pinMode(led6,OUTPUT); 
  for(char i=8;i<14;i++)
  {
   digitalWrite(i,HIGH);  
  }  
}
 
 
void loop()
{
 digitalWrite(led1,LOW);
 delay(100);
 digitalWrite(led1,HIGH);
 digitalWrite(led2,LOW);
 delay(100);
 digitalWrite(led2,HIGH);
 digitalWrite(led3,LOW);
 delay(100);
 digitalWrite(led3,HIGH);
 digitalWrite(led4,LOW);
 delay(100);
 digitalWrite(led4,HIGH);
 digitalWrite(led5,LOW);
 delay(100);
 digitalWrite(led5,HIGH);
 digitalWrite(led6,LOW);
 delay(100);
 digitalWrite(led6,HIGH);
}

 

Links

 

keyestudio Multi-purpose shield V2 for arduino starter

Share

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

Arduino PWM example

In this example we connect an LDR to analog 0 and depending on the value read in we then vary the brightness of an LED connected to Pin 9 using PWM.

The input read from the analog pins will be in the range 0 to 1023. But the PWM function has a the width parameter ranging from 0 to 25 so in this case we use the map() function to convert the values ranging from 0-1023 to 0-255.

Schematic

arduino pwm led

arduino pwm led

Code


const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int PWMOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup()
{
}

void loop()
{
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(PWMOutPin, outputValue);
// wait 2 milliseconds for the a/d converter to settle
delay(2);
}

Links

Share

Switch an LED on and off using the internet

This example requires an ethernet shield and an LED connected to pin 6.

Copy the code into a new sketch and upload it to your arduino and then open your web browser to http://192.168.1.177

 

Code


#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h>
Servo myservo;  // create servo object to control a servo

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

String readString;

//////////////////////

void setup(){

pinMode(6, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
Serial.begin(9600);
Serial.println(“server LED test 1.0″); // so I can keep track of what is loaded
}

void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == ‘\n’) {

///////////////
Serial.println(readString); //print to serial monitor for debuging

client.println(“HTTP/1.1 200 OK”); //send new page
client.println(“Content-Type: text/html”);
client.println();

client.println(“<HTML>”);
client.println(“<HEAD>”);
client.println(“<TITLE>LED on/off</TITLE>”);
client.println(“</HEAD>”);
client.println(“<BODY>”);
client.println(“<H1>LED On/Off</H1>”);
client.println(“<hr />”);
client.println(“<br />”);

client.println(“<a href=\”/?lighton\”\”>Turn On Light</a>”);
client.println(“<a href=\”/?lightoff\”\”>Turn Off Light</a><br />”);

client.println(“</BODY>”);
client.println(“</HTML>”);

delay(1);
//stopping client
client.stop();

///////////////////// control arduino pin
if(readString.indexOf(“?lighton”) >0)//checks for on
{
digitalWrite(6, HIGH);    // set pin 4 high
Serial.println(“Led On”);
}
else
{
if(readString.indexOf(“?lightoff”) >0)//checks for off
{
digitalWrite(6, LOW);    // set pin 4 low
Serial.println(“Led Off”);
}
}
//clearing string for next read
readString=””;

}
}
}
}
}

Links

Ethernet shield at Amazon UK

Ethernet shield at Amazon

Share