Don't overvolt it.

Permalink

I’ll be honest, I’ve been having a little trouble coming up with ideas for these labs. I ended up tinkering with a lot of different sensors and outputs (photo resistor, temperature sensor, servo) and finally settled on getting a handle on how the RGB LED works. Total time, about 4 hours, but that was going through and tinkering with a few different circuits. 

Code: 

int potpin = 2;              // POT connected to digital pin 2 - pos/neg are left and right connections on POT
int rpin = 9;                // Red
int gpin = 10;               // Green
int bpin = 11;               // Blue
float h;                     // hue range
int h_int;                   // Hue color
int r = 0, g = 0, b = 0;           // Default RGB values

int val = 0;                   // Set POT value to default 0

void h2rgb(float h, int& R, int& G, int& B); // Instantiate h2rgb and it's variables  a.k.a  Hue to RGB

void setup()                   
{
  Serial.begin(9600);          // Begin the output of data to serial
}


void loop()                     
{
  val = analogRead(potpin);    // Read the pin and display the value
 h = ((float)val)/1024;       // Get the range. pot value / 1024

  h_int = (int) 360*h;         // Get the color hue by multiplying by 360

  h2rgb(h,r,g,b);              // Call the h2rgb function passing it the hue value

  Serial.print("POT value: ");
  Serial.print(val);           // Pot value
  Serial.print(" = hue of ");
  Serial.print(h_int);         // Color Hue value
  Serial.print(" degrees. RGB values: ");
  Serial.print(r);             // Red value
  Serial.print(" ");
  Serial.print(g);             // Green value
  Serial.print(" ");
  Serial.println(b);           // Blue value

  analogWrite(rpin, r);        // Changes red led
  analogWrite(gpin, g);        // Changes green led
  analogWrite(bpin, b);        // Changes blue led
  
}

void h2rgb(float h, int& R, int& G, int& B) {

  // Used HSV --> RGB function
  // HSV - Hue, Saturation, Value
  // RGB - Red, Green, Blue - example (255,255,255)
  // Function below does a bunch of math to convert HSV values to RGB
  int var_i;
  float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;

  if ( S == 0 )                       //HSV values = 0 ÷ 1
  {
    R = V * 255;
    G = V * 255;
    B = V * 255;
  }
  else
  {
    var_h = h * 6;
    if ( var_h == 6 ) var_h = 0;      //H must be < 1
    var_i = int( var_h ) ;            //Or ... var_i = floor( var_h )
    var_1 = V * ( 1 - S );
    var_2 = V * ( 1 - S * ( var_h - var_i ) );
    var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );

    if      ( var_i == 0 ) {
      var_r = V     ;
      var_g = var_3 ;
      var_b = var_1 ;
    }
    else if ( var_i == 1 ) {
      var_r = var_2 ;
      var_g = V     ;
      var_b = var_1 ;
    }
    else if ( var_i == 2 ) {
      var_r = var_1 ;
      var_g = V     ;
      var_b = var_3 ;
    }
    else if ( var_i == 3 ) {
      var_r = var_1 ;
      var_g = var_2 ;
      var_b = V     ;
    }
    else if ( var_i == 4 ) {
      var_r = var_3 ;
      var_g = var_1 ;
      var_b = V     ;
    }
    else                   {
      var_r = V     ;
      var_g = var_1 ;
      var_b = var_2 ;
    }

    R = (1-var_r) * 255;                  //RGB results = 0 ÷ 255
    G = (1-var_g) * 255;
    B = (1-var_b) * 255;
  }
}

Comments
Permalink

PIxD Lab 2: Analogue Input

Code:

//FSR Pin

int forcePin = 0; //the analog pin the FSR is 

                  //connected to

//LED Pin

int ledPin = 9;   //the pin the LED is connected to

           

int ledPin2 = 11;

void setup()

{

  pinMode(ledPin, OUTPUT); //sets the led pin to output

  pinMode(ledPin2, OUTPUT); //sets second

}

 /*

 * loop() - this function will start after setup 

 * finishes and then repeat

 */

void loop()

{

 int forceLevel = analogRead(forcePin); //Read the

                                        // forcelevel

 forceLevel = map(forceLevel, 0, 900, 0, 255); 

         //adjust the value 0 to 900 to

         //span 0 to 255

 forceLevel = constrain(forceLevel, 0, 255);//make sure the 

                                           //value is between 

                                           //0 and 255

 analogWrite(ledPin, forceLevel);  //write the value

 analogWrite(ledPin2, 255 - forceLevel); //for second pin

}

Comments
Permalink

Lab Wk 2: Questions

Question 1:

With a 9V battery and a 10k resistor, the current passing through the resistor would be .9 mA. 

Question 2:

Comments
Permalink
Components and symbols in the Arduino experimentation kit. (click to enlarge)

Components and symbols in the Arduino experimentation kit. (click to enlarge)

Comments
Permalink
Comments
Permalink

PIxD Lab 1b: Variation on Digital Input/Output - Push Button to Toggle LEDs

const int LED = 13; // the pin for the LED
const int LED2 = 12; // the pin for the second LED
const int BUTTON = 7; //the input pin where the pusbutton is connected

int val = 0; //val will be used to store the state of the input pin
int old_val = 0; // this variable stores the previous value of "val"
int state = 0; // 0 = LED off and 1 = LED on

void setup () {
  pinMode(LED, OUTPUT); // tell Arduino LED is an output
  pinMode(LED2, OUTPUT); // tell Arduino LED2 is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an input
}

void loop() {
  val = digitalRead(BUTTON); //read input value and store it

  //check if there was a transition
  if ((val == HIGH) && (old_val == LOW)) {
    state = 1 - state;
    delay(10);
  }

  old_val = val; // val is now old, store it

  if (state == 1) {
    digitalWrite(LED, HIGH); //turn LED on
    digitalWrite(LED2, LOW); //turn LED2 off

  } else {
    digitalWrite(LED, LOW); // turn LED off
    digitalWrite(LED2, HIGH); //turn LED2 on
  }
}

Comments
Permalink

PIxD Lab 1A: Turn on LED while button is pressed


const int LED 13   // the pin for the LED
const int BUTTON 7 // the input pin where the pushbutton is connected

int val = 0;    // val will be used to store the state of the input pin

void setup() {
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an input
}

void loop(){
  val = digitalRead(BUTTON); // read input value and store it check whether the input is HIGH (button pressed)
  if (val == HIGH) {
    digitalWrite(LED, HIGH); // turn LED ON
  } else {
    digitalWrite(LED, LOW);
  }
}

Comments