Sunday, October 9, 2016

Project 5- Buttons!

Project 5 – Buttons

Our first use of a digital input, a push button to control a LED.
Molly pushing the button

Hardware concepts we learned were:
  • ·         How a push button can regulate voltage and send signals to the microcontroller
  • ·         Why a push button needs more resistance than an LED, and the use of a “pull-up” resistor to prevent false readings from the button



Programming concepts we learned were:
  1. ·         Logic Operations (==,!=,&&,||,!) to build if..then statements
  2. ·         digitalRead for a digital input


Our Program-  If you push one of the buttons, light up the LED.  If you press both at the same time, turn it off.

const int button1Pin = 2;  // pushbutton 1 pin
const int button2Pin = 3;  // pushbutton 2 pin
const int ledPin =  13;    // LED pin
void setup()
{  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(ledPin, OUTPUT); 
}
void loop()
{  int button1State, button2State;
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  if (((button1State == LOW) || (button2State == LOW)) 
      ((button1State == LOW) && (button2State == LOW)))
    digitalWrite(ledPin, HIGH);
  }  else  {    digitalWrite(ledPin, LOW);    }

}

Project 4: Multiple LED's

Project 4 –Multiple LED’s -  8 lights at a time!

Programming concepts we learned were:
  • ·         Managing lots of variables with arrays.
  • ·         Random number generation


Our 3 programs:
  1. 1.    Light all 8 LED’s one by one
  2. 2.    Have the lights “chase”each other like on a marquee
  3. 3.    Light up a random light


int ledPins[] = {2,3,4,5,6,7,8,9};
void setup()
{  int index;
    for(index = 0; index <= 7; index++)
  {    pinMode(ledPins[index],OUTPUT); }}


void loop()
{  oneAfterAnotherLoop() 
  chasing();            
  randomLED();         }

void oneAfterAnotherLoop()
{  int index;
  int delayTime = 100;
  for(index = 0; index <= 7; index++)
  {    digitalWrite(ledPins[index], HIGH);
    delay(delayTime)    
  }                                 

  for(index = 7; index >= 0; index--)
  {    digitalWrite(ledPins[index], LOW);
    delay(delayTime);}            
}

void chasing()
{  int index;
  int delayTime = 200;                  
  for(index = 0; index <= 3; index++)   {
    digitalWrite(ledPins[index], HIGH);   
    digitalWrite(ledPins[index+4], HIGH); 
    delay(delayTime);                     
    digitalWrite(ledPins[index], LOW);   
    digitalWrite(ledPins[index+4], LOW);    }}

void randomLED()
{  int index;
  int delayTime;
    index = random(8);       
  delayTime = 100;
            digitalWrite(ledPins[index], HIGH); 
  delay(delayTime);                   
  digitalWrite(ledPins[index], LOW);  

}

Project 3 - RGB LED's

Project 3 –Lighting up RGB LED’S

Molly explaining to me that her Pony has RGB LED's in
 them that use
similar programs that we wrote.
Hardware concepts we learned were:
  • ·         How a RGB LED has a Red, Green, and Blue diode that can be used together to create any possible color
  •  


Programming concepts we learned were:
  • ·         Constant vs global vs local variables
  • ·         Using subroutines (functions)
  • ·         Using a For..To.. Loop with conditional statements


Our Program-  2 different programs.  The first one lights up each of the 9 “main” colors one at a time.  Then a “rainbow” program that goes though 768 different colors one by once, gradually going through the rainbow, turning on and off faster than the human eye can detect.

const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int DISPLAY_TIME = 100; 
void setup()
{ pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);}

void loop()
{mainColors();
  showSpectrum();}

void mainColors()
{  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);
    digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);}

 showSpectrum()

void showSpectrum()
{  int x; 
    for (x = 0; x < 768; x++)
  { showRGB(x);
    delay(10);     }}



void showRGB(int color)
{  int redIntensity;
  int greenIntensity;
  int blueIntensity;

  if (color <= 255)          // zone 1
  {    redIntensity = 255 - color;    // red goes from on to off
    greenIntensity = color;        // green goes from off to on
    blueIntensity = 0;             // blue is always off
  }
  else if (color <= 511)     // zone 2
  {    redIntensity = 0;                     // red is always off
    greenIntensity = 255 - (color - 256); // green on to off
    blueIntensity = (color - 256);        // blue off to on
  }
  else // color >= 512       // zone 3
  {    redIntensity = (color - 512);         // red off to on
    greenIntensity = 0;                   // green is always off
    blueIntensity = 255 - (color - 512);  // blue on to off
  }
  analogWrite(RED_PIN, redIntensity);
  analogWrite(BLUE_PIN, blueIntensity);

  analogWrite(GREEN_PIN, greenIntensity);}

Project 2 - Potentiometer with Led

Project 2 – Pentiometer with LED
The blue knob in the back is the potentiometer



We modified our first project to include a potentiometer, which we used to control the blink rate of the LED instead of the program we wrote.


Hardware concepts we learned were:

  • ·         How a pentiotiometer can control the voltage to the output.
  • ·         The difference between an Analog and Digital output

Programming concepts we learned were:

  • ·         What a variable is and how to declare and use them
  • ·         Using different data types (Int)
  • ·         How to read an analog input with analogRead

Our Program-  The delay function is linked to the value coming from the potentiometer, so the more we turn the knob, the faster it blinks.


int sensorPin = 0  
int ledPin = 13
void setup()
{ pinMode(ledPin, OUTPUT)}
void loop()
{ int sensorValue
  sensorValue = analogRead(sensorPin)
  digitalWrite(ledPin, HIGH
  delay(sensorValue)
    digitalWrite(ledPin, LOW)

  delay(sensorValue)}

Project 1 - LED Blink



Our very first project was make a circuit with an LED and write a program to have it blink.

Hardware concepts we learned were:
  • ·         How a breadboard allows us to temporarily create circuits without soldering
  • ·         How electricity(5V)  flows from the computer USB to the Arduino, and from the Arduino to the breadboard.
  • ·         The importance of using grounds to complete circuits
  • ·         How to send information from the Arduino output pin to the LED with a lead.
  • ·         Why we need to use a resistor with an LED to limit the current


Programming concepts we learned were:
  1. ·         What is means to compile and upload a program.
  2. ·         The purpose of the setup() and loop() functions
  3. ·         How to configure a pin as in input or output with pinMode()
  4. ·         How to turn on and off current from the pin with digitalWrite()
  5. ·         How to slow down the program processing with delay ()


Our Program-  This program repeatedly turns on the LED connected to Pin 13, keeps it on for 1 second, then turns it off for one second

void setup()
{pinMode(13, OUTPUT)}

 void loop()
{digitalWrite(13, HIGH)
delay(1000)
digitalWrite(13, LOW)

  delay(1000)}