Bethany’s Arduino Homework 2

all circuits

Code for button controlled fade circuit

const int buttonPin = 2; //push button pin number
const int ledPin = 11; //LED pin number

int brightness = 0;    // how bright the LED is
int fadeAmount1 = 5;    // how many points to fade the LED by
int fadeAmount2 = 30;
int buttonState = 0;  

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(buttonPin, INPUT);

  // declare pin 9 to be an output:
  pinMode(ledPin, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {

    // read the input pin:
  int buttonState = digitalRead(buttonPin);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
  
  // set the brightness of pin 9:
  analogWrite(ledPin, brightness);

  if (buttonState == HIGH) {
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount1;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness &gt;= 255) {
    fadeAmount1 = -fadeAmount1;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);

 } else {
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount2;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness &gt;= 255) {
    fadeAmount2 = -fadeAmount2;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
  
 }
}