Fritzing Sample Sketch

By Agustin April 11, 2011

Fritzing = spiffy

I was having a lot of trouble visualizing what I need to do to get some of the things I want to create all rigged up correctly on a breadboard, not to mention knowing what parts I need. I found this awesome site, Fritzing.org which really helps! You can download their program, which lets you make super slick looking layouts visually (or as a schematic, if that is easier for you!). So at least now I can make professional looking circuits to show you guys, and hopefully someone can tell me if they will make teh robut asplode or not! To get the hang of it, I made a schematic for a little circuit I set up to play with an RGB LED.
Schematic after the bump!

Arduino Sample Sketch

This sketch pulsates the RGB channels on a LED at different rates. Button 1 lets you toggle channels on and off, and Button 2 changes the maximum intensity allowed.

To make this you need:

  • Arduino Uno
  • A breadboard
  • 1 RGB LED
  • 10 wires
  • Five 330 ohm resistors
  • Two pushbuttons

Layout your breadboard like this (thanks Fritzing)!
rgb_led_2_buttons


Here is the code for the little circuit.


/*
This lets you mess with an RGB LED and Two buttons.
It is curently set up so that the brightness fluctuates

Cycles through the colors of a RGB LED

Based on RGB_LED_Color_Fade_Cycle.pde
Written for SparkFun Arduino Inventor's Kit CIRC-RGB

*/
/* VERBOSE? Prints to serial monitor */
const boolean VERBOSE = true;
// LED leads connected to PWM pins
const int RED_LED_PIN = 3;
const int GREEN_LED_PIN = 5;
const int BLUE_LED_PIN = 6;

/* Button pins */
const int BTN_1_PIN = 8;
const int BTN_2_PIN = 9;

/* Thresholds for intensity of LED */

const int MAX_INTENSITY = 255; //Full brightness
const int MIN_INTENSITY = 5; //Minimum Brightness

// Used to store the current intensity level of the individual LEDs
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;
// All of those levels are controlled by this one. See the loop code.
int intensity = 255;
//Channel on/off state - 0 = off, 1 = on
int redState = 1;
int greenState = 1;
int blueState = 1;

//The bounced the master intensity between MIN_ and MAX_INTENSITY
int dir = -1;

//This variables represents time - +1 each tick. Used for Trig.
int t = 0;

// Length of time we spend showing each color
const int DISPLAY_TIME = 50; // In milliseconds

/* States to toggle thru RGB on/off */
int states[6][3] = {{0,0,1},
{0,1,0},
{0,1,1},
{1,0,0},
{1,0,1},
{1,1,1}};
int currState = 5; //Set to the one above, all colors on.
int numStates =6;

//Used to cause the button to make an action once each time it is pressed.
boolean btn1_DOWN = false;

//SETUP THE BUTTONS
void setup() {
Serial.begin(9600);
pinMode(BTN_1_PIN, INPUT);
pinMode(BTN_2_PIN, INPUT);
}

void loop() {
int btn1 = digitalRead(BTN_1_PIN);
int btn2 = digitalRead(BTN_2_PIN);

/*Change active colors
Only do this once each time the button is pressed and released
*/
if(btn1 == LOW) {

btn1_DOWN = true;

} else if(btn1 == HIGH){

if(btn1_DOWN == true) {

btn1_DOWN = false;

currState++;

if (currState == numStates) {
currState = 0;
}

redState = states[currState][0];
greenState = states[currState][1];
blueState = states[currState][2];

if (VERBOSE) {
Serial.print("::: redState ");
Serial.print(redState);
Serial.print(" greenState ");
Serial.print(greenState);
Serial.print(" blueState ");
Serial.print(blueState);
Serial.println();
}
}
}

/*CHANGE INTENSITY
Changed continuously while button is pressed.
*/
if(btn2 == LOW) {
if (dir == -1) {
intensity-=5;
if(intensity <= MIN_INTENSITY) { intensity = MIN_INTENSITY; dir = 1; } } else { intensity+=5; if(intensity >= MAX_INTENSITY) {
intensity = MAX_INTENSITY;
dir = -1;
}
}
if (VERBOSE) {
Serial.print(" INTENSITY::");
Serial.print(intensity);
Serial.println();
}
}
//Use trig functions to make the channels pulse at different rates.
redIntensity = abs(int(intensity * sin(t*0.01)));
greenIntensity = abs(int(intensity * cos(t*0.02)));
blueIntensity = abs(int(intensity * sin(t*0.03)));

analogWrite(RED_LED_PIN, redIntensity * redState);
analogWrite(GREEN_LED_PIN, greenIntensity * greenState);
analogWrite(BLUE_LED_PIN, blueIntensity * blueState);

delay(DISPLAY_TIME);
t++;

}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *