270d Final Project

From CSclasswiki
Jump to: navigation, search

Proposal - Overview

Arduino-Only Project - The goal of this project is to integrate three Elegoo kit sensors on the Arduino and design a program in which they work together.

The three sensors and their functions:

  • Part 1: I will use a 64-LED matrix that will display "E I L E E N".
  • Part 2: I will use Green, Red, and Yellow LEDs that will function as traffic lights.
    • If the red light is on then the matrix will display a stop sign.
    • If the green light is on then the matrix will display an arrow.
    • If the yellow is on then the matrix will display a diamond.
  • Part 3: I will use a light sensor as the third device that will function as an indicator if it's dark or light in the surrounding. I will integrate the 64-LED matrix and three LEDs by:
    • If the light sensor picks up that it is dark in the surrounding then the red LED will flash at a higher speed and the 64-LED matrix will display a proceed with caution sign.
    • If the light sensor picks up that it is light out then the green and yellow LED will flash one at a time at a slower speed and the 64-LED matrix will display an arrow.

Plan

  1. Make sure I have all the materials I need for each implementation.
  2. Write sketches and build circuits on the Arduino for each device to verify that each work properly on their own
    1. Part 1 - 64-LED matrix
    2. Part 2 - Green, Yellow, and Red LEDs
    3. Part 3 - Light sensor
  3. Integrate all devices together on the Arduino and write a sketch
  4. Update Wiki page to show progress on each part of this assignment.

Material

Hardware

  1. Arduino Mega2560
  2. Wires
  3. Resistors
  4. BreadBoard
  5. 64-LED Matrix
  6. Green, Red, and Yellow LEDs
  7. Photoresistor (Light Sensor)

Software

I will program in C for my sketches that will be uploaded to the Arduino through the Arduino IDE.

Part 1: Implementation of the 64-LED Matrix

Summary

I used C in Arduino and a 64-LED Matrix in order to create a sketch that displays my name 'E I L E E N'.

The sketch functions setting the pins, the bytes in hexadecimal for each letter. Then using a function called printLetter allows us to setRows or light up the red LED for the byte 'E'. We will loop by displaying each letter on the 64-LED matrix.

Images/Videos

Display of name 'E I L E E N'

E-letter.jpeg I-letter.jpeg L-letter.jpeg E2-letter.jpeg E3-letter.jpeg N-letter.jpeg


The following schematics was used

SetUp.png



Sketch

//Eileen Asinc Luna
//Part 1 - Implementation of the 64-LED Matrix
//Will display "E I L E E N"

//Library needed for 64 LED Matrix
#include "LedControl.h" 

//variable for pins
int DIN = 12;
int CS =  11;
int CLK = 10;

//byte for letters that will show on matrix
byte E[8]={0x00,0x7c,0x40,0x78,0x40,0x7c,0x00,0x00};
byte I[8]={0x00,0x7c,0x10,0x10,0x10,0x7c,0x00,0x00};
byte L[8]={0x00,0x40,0x40,0x40,0x40,0x7c,0x00,0x00};
byte N[8]={0x00,0x44,0x64,0x54,0x4c,0x44,0x00,0x00};

LedControl lc=LedControl(DIN,CLK,CS,0);

//Function that will display bytes
void printLetter(byte character [])
{
  int i = 0;
  for(i=0;i<8;i++)
  {
    lc.setRow(0,i,character[i]);
  }
}

//Function for delay
void next(){
  delay (1000);
}


void setup() {
 lc.shutdown(0,false);      
 lc.setIntensity(0,2);      
 lc.clearDisplay(0);
}

//loop will call function to display byte followed by a delay
void loop() 
  printLetter(E);
  next();
  printLetter(I);
  next();
  printLetter(L);
  next();
  printLetter(E);
  next();
  printLetter(E);
  next();
  printLetter(N);
  next();
}

Part 2: Implementation of the three LEDs

Summary

I used C in Arduino, three LEDs (green, red, and yellow), and a 64-LED Matrix in order to create a sketch for a finite state machine.

The sketch functions by using what we already learned in Part 1 where we add 3 LEDs that go from green to yellow to red based on the state it is on. I set the pins for the three LEDs and used a function called trafficLights that, for example, for state 00 will have green as high while the rest are low and it would also call the function printByte(goArrow) to display an arrow on the 64-LED.

Images/Videos

Green to Yellow to Red or Go to Caution to Stop

Green.jpeg Yellow.jpeg Red.jpeg


Schematics

SchematicsImagePart2.png


Video of traffic lights

Sketch

//Eileen Asinc Luna
//Part 2: Implementation of the three LEDs
//Will go from green to yellow to red based on the state
//It will additionally change the 64-led matrix 

//Library for 64-LED matrix
#include "LedControl.h" 

//pins for Matrix
int DIN = 12;
int CS =  11;
int CLK = 10;

//pins for Three LEDs
int redPin = 41;
int yellowPin = 45;
int greenPin = 49;

//byte representation for signs 
byte stopSign[] = {B00000000,B00011000,B00111100,B01111110,B01111110,B00111100,B00011000,B00000000};
byte goArrow[] = {B00000000,B00011000,B00011000,B00111100,B01111110,B00011000,B00011000,B00011000};
byte caution[] = {B00000000,B00011000,B00100100,B01000010,B10000001,B01000010,B00100100,B00011000};

//initialization of the state for the FSM
int state = 0;

LedControl lc=LedControl(DIN,CLK,CS,0);

//Function will display byte of each sign
void printSign(byte character [])
{
  int i = 0;
  for(i=0;i<8;i++)
  {
    lc.setRow(0,i,character[i]);
  }
}

//Function for delay
void next(){
  delay (1000);
}

//Function for Traffic Light FSM
void trafficLights( int s ) {
  //State 00  green - on and yellow - off and red - off, Will additionally also display Go Arrow sign
  if(state == 0) {
    digitalWrite(greenPin, HIGH);
    digitalWrite(yellowPin, LOW);
    digitalWrite(redPin, LOW);
    printSign(goArrow);
  }
  
  //State 01  green - off and yellow - on and red - off, will additionally also display Caution sign
  else if(state == 1) {
    digitalWrite(greenPin, LOW);
    digitalWrite(yellowPin, HIGH);
    digitalWrite(redPin, LOW);
    printSign(caution);
  }
  
  //State 11 green - off and yellow - off and red - on, will additionally display Stop sign
  else if(state == 2) {
    digitalWrite(greenPin, LOW);
    digitalWrite(yellowPin, LOW);
    digitalWrite(redPin, HIGH);
    printSign(stopSign);
  }
}


void setup() {
  //output for red, yellow, green pins
  pinMode( redPin, OUTPUT );
  pinMode( yellowPin, OUTPUT );
  pinMode( greenPin, OUTPUT ); 
  
  lc.shutdown(0,false);      
  lc.setIntensity(0,2);      
  lc.clearDisplay(0);   
}

//calls function trafficLights function
void loop() {
  trafficLights( state );
  delay( 3000 );
  state = (state + 1) % 3;
}

Part 3: Implementation of the Photoresistor

Summary

I used C in Arduino, a photoresistor (light sensor), and a green LED.

The sketch functions by using what we already learned in Part 2 where I implemented LEDs that change from LOW to HIGH. I set the pins for the LED and the photoresistor and read the value from the photoresistor and if greater or less than 50 will change the state of the LED from on to off.

Images/Videos

Visual for the light sensor when the value is greater than 50.

LightOff.jpeg


Visual for the light sensor when the value is less than 50. Note: I can either turn the lights off or place a finger over the sensor.

LightUp.jpeg SecondLighton.jpeg


The following schematics was used

SetUpForPart3.jpg



Sketch

//Eileen Asinc Luna
//Part 3 - Implementation of the photoresistor
//When the photoresistor detects the value to be greater than 50 then the LED will be set to LOW 
//and when it is less then 50 then the LED will be set to high
//In other words when there is light the LED will be off and when its dark the LED will be on

//set pin to variables
int lightSensorPin = A0;
int greenPin = 29;

//more variables for value 
int analogValue;

void setup() {
  pinMode( greenPin, OUTPUT ); 
  pinMode(lightSensorPin, INPUT);
}

//will loop on checking the value given by the photoresistor
void loop() {
  analogValue = analogRead(lightSensorPin);
  if(analogValue > 50){            
    digitalWrite(greenPin, LOW);
    
  }else{
     digitalWrite(greenPin, HIGH);
  }
}

Part 4: Integration of the three devices

Summary

I used C in Arduino, a photoresistor, three LEDs (green, red, and yellow), and a 64-LED Matrix in order to create a sketch for a finite state machine.

The sketch functions by using what we already learned in Part 1-3. The analog read will get a value and if it is greater than 50 then it will call a function cautionLights that will act as a two state FSM that will alternatively turn off and on the red and yellow LED while the green LED is always off, it will additionally display a stop sign on the 64LED- matrix. If the value is more than 50 then it will call a function that will act as a two state FSM that will act as a blinking green LED turning off and on with a second delay and additionally display a go arrow on the 64-LED matrix.

Images/Videos

Visual for the light sensor when the value is greater than 50.

PicturePart1.jpeg


Visual for the light sensor when the value is greater than 50.

PicturePart2.jpeg PicturePart3.jpeg


Schematics

SchematicsImagePart4.png




Sketch

//Eileen Asinc Luna
//Part 4 - Implementation of the three sensors
//The analog read will get a value and if it is greater than 50 then it will call a function cautionLights that will act as a two state FSM 
//that will alternatively turn off and on the red and yellow LED while the green LED is always off, it will additionally display a stop 
//sign on the 64LED- matrix. If the value is more than 50 then it will call a function that will act as a two state FSM that will act 
//as a blinking green LED turning off and on with a second delay and additionally display a go arrow on the 64-LED matrix.

//Library needed for 64-LED Matrix
#include "LedControl.h" 

//Pins for 64-LED Matrix
int DIN = 12;
int CS =  11;
int CLK = 10;

//variable for photoresistor pin and value it reads
int lightSensorPin = A0;
int analogValue;

//variable for state
int state = 0;

//pins for three LEDs
int redPin = 35;
int yellowPin = 37;
int greenPin = 41;

//bytes for signs
byte stopSign[] = {B00000000,B00011000,B00111100,B01111110,B01111110,B00111100,B00011000,B00000000};
byte goArrow[] = {B00000000,B00011000,B00011000,B00111100,B01111110,B00011000,B00011000,B00011000};

LedControl lc=LedControl(DIN,CLK,CS,0);

//Function that will display signs on the 64-LED matrix
void printByte(byte character [])
{
  int i = 0;
  for(i=0;i<8;i++)
  {
    lc.setRow(0,i,character[i]);
  }
}

//Function when anolog read is greater than 50 which 
//Will have a two state FSM from red to yellow and will display stop sign
void cautionLights( ) {
  printByte(stopSign);
  if(state == 0) {
    digitalWrite(greenPin, LOW);
    digitalWrite(yellowPin, HIGH);
    digitalWrite(redPin, LOW);
  }
  else if(state == 1) {
    digitalWrite(greenPin, LOW);
    digitalWrite(yellowPin, LOW);
    digitalWrite(redPin, HIGH);
  }
}

//Function when anolog read is less than 50 which 
//Will have a two state FSM from red to yellow and will display go sign
void goLights( ) {
    printByte(goArrow);
    if(state == 0) {
    digitalWrite(yellowPin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
  }
  else if(state == 1) {
    digitalWrite(greenPin, HIGH);
    digitalWrite(yellowPin, LOW);
    digitalWrite(redPin, LOW);
  }

}

void setup() {
  pinMode( redPin, OUTPUT );
  pinMode( yellowPin, OUTPUT );
  pinMode( greenPin, OUTPUT ); 
  
  pinMode(lightSensorPin, INPUT);

  lc.shutdown(0,false);      
  lc.setIntensity(0,1);      
  lc.clearDisplay(0); 
}

void loop() {
  //will read value from photoresistor 
  analogValue = analogRead(lightSensorPin);
  if(analogValue > 50){            
    goLights();
  }
  else{
    cautionLights();
  }
  delay(1000);
  state = (state + 1) % 2;
}

Progress Log

  • 4/26/20
    • Completed Part 1 by successfully implementing a sketch on the Arduino to display 'E I L E E N'.
    • Completed Part 2 by successfully implementing a sketch on the Arduino to use a finite state machine to go from red, yellow, and green
  • 4/30/20
    • Added the sketches to part 1 and part 2
    • Completed Part 3 by successfully implementing a sketch on the Arduino to use a photoresistor or light sensor that is able to detect darkness
  • 5/2/20
    • Edited Part 1-3
  • 5/3/20
    • Completed Part 4 by successfully implementing a sketch on the Arduino to to integrate three sensors.
  • 5/5/20
    • Added schematics for Part 1-4
  • 5/7/20
    • Added References and put finishing touches on the wiki

References

How to use 64-LED and display bytes

Schematics for Part 1

Schematics for Part 3