Arduino board and code


Arduino Board and Code.

The arduino is a great piece of kit with a very helpful community.  This code is for 4 potentiometers, each controlling the intensity of one LED via PWM 8bit control.  This then links back to the laptop via a push button to give a readout of the values via the serial port.  In this instance, I have Yellow, White, green and Orange LEDs to try to match the colour of the tallow candlelight.

Anyway, here is the code: (The bits with “//” before them is an explanation of what the code means.  I usually write this in to my code to help me understand wheat each line does.  It helps when trying to fix errors too!)

//set up pots
int pot1 = 0;                   //These are the analogue numbers on the arduino board
int pot2 = 1;
int pot3 = 2;
int pot4 = 3;
int pot5 = 4;
int button = 5;
//set up LEDs
int LED1 = 9;                       //These are the digital numbers on the arduino board
int LED2 = 10;
int LED3 = 11;
int LED4 = 3;
int LED5 = 5;

int oneVal = 0;                // Variables to store the input from the potentiometers
int twoVal = 0;
int threeVal = 0;
int fourVal = 0;
int fiveVal = 0;
int buttonval = 0;

void setup()
{
pinMode(LED1, OUTPUT);   // sets the digital pins as output
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);     // Open serial communication for reporting
}
void loop()
{

oneVal = (analogRead(pot1)/4);  // read input pins, convert to 0-255 scale
twoVal = (analogRead(pot2)/4);
threeVal = (analogRead(pot3)/4);
fourVal = (analogRead(pot4)/4);
fiveVal = (analogRead (pot5)/4);

analogWrite(LED1, oneVal);    // Send new values to LEDs
analogWrite(LED2, twoVal);
analogWrite(LED3, threeVal);
analogWrite(LED4, fourVal);
analogWrite(LED5, fiveVal);

buttonval = analogRead(button); //Test Button Pin for high or low

if (buttonval>1000)   // Is button pin at 5v or 0?
{
delay(1000);                          //delay for 1 second so that you only get one line of data.
Serial.print(“Yellow: “);        // …then print the values.
Serial.print(oneVal);
Serial.print(“t”);                    //tab
Serial.print(”   White: “);        //call this white
Serial.print(twoVal);              //print the value for white
Serial.print(“t”);
Serial.print(”   Green: “);
Serial.print(threeVal);
Serial.print(“t”);
Serial.print(”   Orange: “);
Serial.print(fourVal);
Serial.print(“t”);
Serial.print(”   Unused: “);
Serial.println(fiveVal);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: