Monday, April 10, 2017

Learning to Code: RGB LED



Next in my Learning to Code series, taking on the RGB LED. First off is a healthy lesson from Adafruit Blog but not for the whole thing! I want to use the Adafruit blog to get my wires set up properly.

The biggest challenge was understanding which pins on the RGB LED belonged to which color. After some helpful diagrams were presented, I learned that I had set my Red to PWM 11, Green to PWM 10, and Blue to PWM 9.

My goal in this challenge is to code as much from scratch as possible, see the outcome and then use the established code for help. But one of my pitfalls so far has been coding from scratch. I feel fairly competent reading code (or at least have a general understanding) but mastery of any language requires doing.


With my breadboard set up for one RGB LED (270 ohm resistors leading out of each pin and the ground attached to the longest wire), time to start coding.

So, then this happened. And failed. I seemed to have been doing ok, I defined all my PWMs, set-up each pin for outputs and tried programing in basic colors. Off to visit the blog at this point to determine what I did incorrectly.

[code]
// RGB LED code- @thatgamingmom with credit to Adafruit https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/overview

int Rpin = 11; //Define PWM for red pin
int Gpin = 10; //Define PWM for green pin
int Bpin = 9; //Define PWM for blue pin


void setup() {
  pinMode (Rpin, OUTPUT); //Define output for red pin
  pinMode (Gpin, OUTPUT); //Define output for green pin
  pinMode (Bpin, OUTPUT); //Define output for blue pin
}

void loop() {
  setColor(255, 0, 0); //red
  delay(1000);
  setColor (0, 255, 0); //green
  delay(1000);
  setColor(0, 0, 255); //blue
  delay(1000);
}

void setColor(I didn't define anything here!!) {
  analogWrite(Rpin, red);
  analogWrite(Gpin, green);
  analogWrite(Bpin, blue);
}
[/code]

Made the fix to void setColor and it's still checking my code and telling me that I'm the worst! Ok. Save my file, import sketch written by adafruit to see if it yells at me.

Ok, so my code sucks, got it. But the light isn't working so I guess it wouldn't have mattered... back to the wiring... which I did wrong, then maybe did right but it still didn't work.


My very patient friend reminded me how breadboards work (thank-you!!) but the end result is my LED still doesn't work.

Then I sent some pictures over to VPF (very patient friend) and while he figured out what the fuck is going on with my life- I drank some.

This will need to be a part II, from what we can figure out, this should work and I stole the code, so I don't think it's my shitty code.

No comments:

Post a Comment