Selfish Teddy

Teddy love his stuffed elephant Paco and he feels very possessive about him. He hates to share. If you take Paco away, watch out!

Here’s the code that produced this:

const int buttonPin = 2; // the number of the pushbutton pin
const int ledRightEye = 9; // the number of the LED pins
const int ledLeftEye = 10;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int buttonState; // variable for reading the pushbutton status

void setup() {
pinMode(ledRightEye, OUTPUT);
pinMode(ledLeftEye, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
// set the brightness of the pins:
analogWrite(ledRightEye, brightness);
analogWrite(ledLeftEye, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(3);
}else{
analogWrite(ledRightEye, 0);
analogWrite(ledLeftEye, 0);
}
}

But before I could even get to that I had to figure out a few things.

My first Arduino program and circuit was to use a button as input and when pressed an LED light would turn on as the output. Having come from somewhat of a development background, programming the Arduino was pretty straight forward. The hard part was figuring out where all these wires went. Eventually it started to make sense and I got it to work.

Here’s the code:

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Next I wanted to try to get the light to pulse. In trying to accomplished I realized with digitalRead and digitalWrite they only took values of HIGH and LOW, meaning on or off. I couldn’t vary the intensity of the light. I had to use analogRead and analogWrite

Here’s the code for this:

int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 9; // the number of the LED pin
int buttonState; // variable for reading the pushbutton status

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

if (buttonState == LOW) {
// set the brightness of the LED:
analogWrite(ledPin, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}else{
analogWrite(ledPin, 0);
}
}

Then as part of the assignment, we had to create a switch. When the button is pressed, one light would turn on and the other would turn off. And vice versa, when the button not pressed.

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPinGreen = 9; // the number of the LED pin
const int ledPinRed = 10;
int buttonState; // variable for reading the pushbutton status

void setup() {
pinMode(ledPinGreen, OUTPUT);
pinMode(ledPinRed, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
// set the brightness of the pins:
digitalWrite(ledPinRed, HIGH);
digitalWrite(ledPinGreen, LOW);
}else{
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinGreen, HIGH);
}
}

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.