PDA

View Full Version : Arduino Sketches



Oneslowz28
05-12-2010, 12:53 PM
This will be a small repository of Arduino Sketches that we have created and shared. To have your sketch listed here it must work. If you are wanting to ask questions about your sketch you should create a new thread. This thread is only for sketches to be posted in. For comments you should post your sketch in a new thread.

Oneslowz28
05-12-2010, 12:53 PM
I have a pretty cool Random color RGB LED fader code you should have fun messing around with. Just change the delays for faster or slower fade time. It works with common anode RGB leds.

It uses pins 9, 10 and 11 and remember to use resistors on each anode. Cathode to Ground.



float RGB1[3];
float RGB2[3];
float INC[3];

int red, green, blue;

int RedPin = 10;
int GreenPin = 11;
int BluePin = 9;


void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));

for (int x=0; x<3; x++) {
RGB1[x] = random(256);
RGB2[x] = random(256); }

}

void loop()
{
randomSeed(analogRead(0));

for (int x=0; x<3; x++) {
INC[x] = (RGB1[x] - RGB2[x]) / 256; }

for (int x=0; x<256; x++) {

red = int(RGB1[0]);
green = int(RGB1[1]);
blue = int(RGB1[2]);

analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
delay(25);

for (int x=0; x<3; x++) {
RGB1[x] -= INC[x];}

}

for (int x=0; x<3; x++) {
RGB2[x] = random(956)-700;
RGB2[x] = constrain(RGB2[x], 0, 255);

delay(10);
}
}

SXRguyinMA
05-12-2010, 10:16 PM
This code with move a servo based on a temperature reading. The temp sensor used is a 2-wire type from an NZXT Sentry 2 fan controller.

it will run the loop only when 5v is sensed on the digital #7 input. this can be easily removed if you don't need this feature.

You'll have to adjust the "val = map" and "myservo.write(#)" values according to your specific usage.

Project here: http://fritzing.org/projects/arduino-temperature-based-servo-control/

schematic: (made using Fritzing (http://fritzing.org/))
http://i92.photobucket.com/albums/l11/sportrider12584/servo_control_wiring.png

// Controlling a servo position using a temperature sensor
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// edited 5-12-2010 by Will Lyon to include base setting for servo if voltage not detected on pin 7
//Huge thanks to crenn @ TBCS for helping me sort this!

#include <Servo.h>
#define CONTROL 7

Servo myservo; // create servo object to control a servo

int temps = 0; // analog pin used to connect the temp sensor
int val; // variable to read the value from the analog pin

void setup()
{
pinMode (CONTROL, INPUT); // sets the control pin to input
myservo.attach(9); // attaches the servo on pin 9 to the servo object
digitalWrite(CONTROL, LOW); // ensure internal pullup resistor is disabled.

}

void loop()
{
val = digitalRead(CONTROL); //read input of pin 7 and store it
if (val == HIGH){ // reads whether or not 5v is present on pin 7, if 5v present, continue:
val = analogRead(temps); // reads the value of the temp sensor (value between 0 and 1023)
val = map(val, 350, 700, 75, 105); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(25); // waits for the servo to get there
} else {
if (val == LOW); // if no voltage present on pin 7, continue
myservo.write(50); //sets servo position to 50 if no voltage is detected on pin 7
}
delay(25); // waits for the servo to get there

}