270k Final Project

From CSclasswiki
Jump to: navigation, search


Project Proposal

Introduction

For my final project, I have chosen the Arduino-Only option. I will use the 8x8 LED Matrix, the joystick module, and the fan blade. I want to display a simple maze on the matrix and have a dot be the "character" that needs to solve the maze, the dot will be controlled by the movement of the joystick and once the maze is solved the fan will turn on.

Process

Here are the steps I will need to take to complete my project

  1. I will find relevant resources on how each component of my project works (the matrix, fan, and joystick)
  2. Integrate Part 1 and display my name on the matrix
  3. Integrate Part 2 and get the joystick to work in a simple way, displaying the movements on the Serial Monitor or move a dot on the matrix
  4. Integrate Part 3 and get the fan to work, controlling it from the sketch.
  5. Create the final product and integrate all parts.

Project Execution

Part 1

For this part of the project I displayed my name and a smiley face on the 8x8 LED Matrix, one character at a time for one second in a loop. I used this tutorial as a reference. Following the tutorial, I downloaded LedControl, an external library for working with the 8x8 LED Matrix. The library can be found in the description of the tutorial or here

Materials and Setup

  • 8x8 LED Matrix
  • 5 Female-to-Male Dupont Wires

Code

The programming language used is C.

/**Alonda Robichaud
 * CSC 270 Final Project
 * Spring 2020
 * Parts: 8x8 LED Matrix
 * Prints out "ALONDA" followed by a smiley face, one character at a time, on a 8x8 LED Matrix
 * External Library: https://github.com/wayoda/LedControl/
**/


//External library for working with the 8x8 matrix
#include <LedControl.h>

int DIN = 12; //digital pin connected to DIN of 8x8 Matrix
int CS = 11; //digital pin connected to CS of 8x8 Matrix
int CLK = 10; //digital pin connected to CLK of 8x8 Matrix

//array of the byte values of each row for each character
byte smile[8] = {B00111100, B01000010, B10100101, B10000001, B10100101, B10011001, B01000010, B00111100};
byte A[8] = {B00011000, B00100100, B01000010, B01000010, B01111110, B01000010, B01000010, B01000010};
byte L[8] = {B01000000, B01000000, B01000000, B01000000, B01000000, B01000000, B01000000, B01111110};
byte O[8] = {B00111100, B01000010, B01000010, B01000010, B01000010, B01000010, B01000010, B00111100};
byte N[8] = {B01000010, B01100010, B01010010, B01001010, B01000110, B01000010, B01000010, B01000010};
byte D[8] = {B01111000, B01000100, B01000010, B01000010, B01000010, B01000010, B01000100, B01111000};

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

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

//print characters
void printChar(byte character[8]){
  lc.setRow(0,0,character[0]);
  lc.setRow(0,1,character[1]);
  lc.setRow(0,2,character[2]);
  lc.setRow(0,3,character[3]);
  lc.setRow(0,4,character[4]);
  lc.setRow(0,5,character[5]);
  lc.setRow(0,6,character[6]);
  lc.setRow(0,7,character[7]);
}

//print each character and wait 1 second before moving to the next
void loop() {
  printChar(A);
  delay(1000);
  printChar(L);
  delay(1000);
  printChar(O);
  delay(1000);
  printChar(N);
  delay(1000);
  printChar(D);
  delay(1000);
  printChar(A);
  delay(1000);
  printChar(smile);
  delay(1000);
}

Schematics

The schematics and wiring diagrams were generated using Fritzing.

Name display wiring.png Name display schematics.png

Results

Below is a video of the 8x8 LED Matrix in action.

Part 2

In this part, I integrated a joystick into the circuit where a dot on the 8x8 Matrix would move according to the joystick movements. I used this tutorial as a guide on how to use a joystick with an Arduino and this tutorial as a reference on how to use the joystick to control the 8x8 LED Matrix. I decided to use the x-coordinate movements of the joystick as vertical movements on the 8x8 Matrix and y-coordinate movements as horizontal as I found it easier to hold both parts when the joystick is rotated

Materials and Setup

  • 8x8 LED Matrix
  • Joystick
  • 10 Female-to-Male Dupont Wires
  • Breadboard

Code

The programming language used is C.

/**Alonda Robichaud
 * CSC 270 Final Project
 * Spring 2020
 * Parts: Joystick, 8x8 LED Matrix
 * Move a dot on the 8x8 LED Matrix using a joystick.
 * External Library: https://github.com/wayoda/LedControl/
**/

//External library for working with the 8x8 matrix
#include <LedControl.h>

int y_move = 0; //initial y position
int x_move = 7; //initial x position

const int SW_pin = 2; //digital pin connected to switch output
const int X_pin = 0; //analog pin connected to x output
const int Y_pin = 1; //analog pin connected to y output

int DIN = 12; //digital pin connected to DIN of 8x8 Matrix
int CS = 11; //digital pin connected to CS of 8x8 Matrix
int CLK = 10; //digital pin connected to CLK of 8x8 Matrix

//direction where the dot will move
int x_dir; 
int y_dir;

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

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(9600);
  lc.shutdown(0, false);
  lc.setIntensity(0,0);
  lc.clearDisplay(0);
}

void loop() {

  //read position of joystick
  x_dir = analogRead(X_pin);
  y_dir = analogRead(Y_pin);

  //move according to joystick, but not off the matrix
  if (x_dir < 20 and x_move > 0){
    x_move -= 1;
    Serial.print("up\n");
  }else if (x_dir > 800 and x_move < 7) {
    x_move += 1;
    Serial.print("down\n");
  }else if (y_dir < 20 and y_move < 7) {
    y_move += 1;
    Serial.print("right\n");
  }else if (y_dir >800 and y_move > 0){
    y_move -= 1;
    Serial.print("left\n");
  }

  lc.setLed(0, x_move, y_move, true);
  
  //print out coordinates of joystick
  Serial.print("Switch: ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
  lc.clearDisplay(0);
}

Schematics

The schematics and wiring diagrams were generated using Fritzing.

Joystick wiring.png Joystick schematics.png

Results

Below is a video demonstrating how it works

Part 3

For this part, I integrated a DC Motor attached with a fan blade into the circuit. A dot on the 8x8 Matrix will randomly change positions every second and if it lands on a specific position, in this case (7,0), the fan will turn on. This is the the tutorial I used as a guide on how to control the DC motor.

Materials and Setup

  • 8x8 LED Matrix
  • 5 Female-to-Male Dupont Wires
  • DC Motor
  • Fan Blade
  • 1K Resistor
  • NPN Transistor S8050
  • Diode Rectifier
  • Breadboard

Code

The programming language used is C.

/**Alonda Robichaud
 * CSC 270 Final Project
 * Spring 2020
 * Parts: DC motor, Fan Blade, 1k Resistor, NPN Transistor, Diode Rectifier
 * Move a dot on the 8x8 Matrix randomly every second and when it lands on (7,0), turn on the fan
 * External Library: https://github.com/wayoda/LedControl/
**/


//External library for working with the 8x8 matrix
#include <LedControl.h>

int DIN = 12; //digital pin connected to DIN of 8x8 Matrix
int CS = 11; //digital pin connected to CS of 8x8 Matrix
int CLK = 10; //digital pin connected to CLK of 8x8 Matrix

int fan = 13;
LedControl lc = LedControl(DIN, CLK, CS, 0);


void setup() {
  pinMode(fan, OUTPUT);
  Serial.begin(9600);
  lc.shutdown(0, false);
  lc.setIntensity(0,1);
  lc.clearDisplay(0);
}

void loop() {
  int randX = random(0,8); //random x coordinate
  int randY = random(0,8); //random y coordinate
  lc.setLed(0, randX, randY, true);
  if (randX == 7 and randY == 0){
    digitalWrite(fan, HIGH);
  }
  delay(1000);
  digitalWrite(fan, LOW);
  lc.clearDisplay(0);
}

Schematics

The schematics and wiring diagrams were generated using Fritzing.

Fan wiring.png Fan schematics.png

Results

Below is a video demonstrating how it works.


Part 4

To tie in all the components used previously, I decided to create a maze solver. The character in the maze will start on the bottom-left corner of the 8x8 Matrix and the joystick will be used to move the character to the upper-right corner. Once the maze is solved the fan will turn on.

Materials and Setup

  • 8x8 LED Matrix
  • Joystick
  • 10 Female-to-Male Dupont Wires
  • DC Motor
  • Fan Blade
  • 1K Resistor
  • NPN Transistor S8050
  • Diode Rectifier
  • Breadboard

Code

The programming language used is C.

/**Alonda Robichaud
 * CSC 270 Final Project
 * Spring 2020
 * Parts: 8x8 LED Matrix, DC motor attached to fan blade, Joystick, 1k Resistor, NPN Transistor, Diode Rectifier
 * A maze solver on Arduino Mega 2560
 * Solve hardcoded maze by moving a dot from the lower-left corner to the upper-right corner using a joystick, once solved the fan will turn on
 * External Library: https://github.com/wayoda/LedControl/
**/

//External library for working with the 8x8 matrix
#include <LedControl.h>

int y_move = 0; //initial y position
int x_move = 7; //initial x position

int row = 7; //initial position in the 2d array
int col = 0; //initial position in the 2d array

int DIN = 12; //digital pin connected to DIN of 8x8 Matrix
int CS = 11; //digital pin connected to CS of 8x8 Matrix
int CLK = 10; //digital pin connected to CLK of 8x8 Matrix

//direction where the dot will move
int x_dir; 
int y_dir;

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

const int SW_pin = 2; //digital pin connected to switch output of joystick
const int X_pin = 0; //analog pin connected to x output of joystick
const int Y_pin = 1; //analog pin connected to y output of joystick

//maze to be solved
bool maze[8][8] = {{false, true, false, false, false, false, false, false},
                 {false, false, false, true, true, true, false, false},
                 {false, true, false, false, false, false, true, false},
                 {false, false, true, false, false, false, true, true},
                 {true, true, true, false, true, true, false, false},
                 {false, false, false, false, false, false, true, false},
                 {false, true, true, false, true, false, false, false},
                 {false, false, false, false, true, false, true, false}};


void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  lc.shutdown(0, false);
  lc.setIntensity(0,0);
  lc.clearDisplay(0);

}

void printMatrix(bool matrix[8][8]){
  for (int i = 0; i<8; i++){
    for (int j = 0; j<8; j++){
      lc.setLed(0, i, j, matrix[i][j]);
    }
  }
}

void loop() {
  //print maze
  printMatrix(maze);
  
  //if solved turn on fan
  if (x_move == 0 and y_move == 7){
    Serial.print("fan");
    digitalWrite(13, HIGH);
  }
  
  //read position of joystick
  x_dir = analogRead(X_pin);
  y_dir = analogRead(Y_pin);

 
  //move according to joystick, but not off the matrix or into a "wall" 
  if (x_dir < 20 and x_move > 0 and maze[row-1][col] == false){
    row -= 1;
    x_move -= 1;
    Serial.print("up\n");
  }else if (x_dir > 800 and x_move < 7 and maze[row+1][col] == false) {
    row += 1;
    x_move += 1;
    Serial.print("down\n");
  }else if (y_dir < 20 and y_move < 7 and maze[row][col+1] == false) {
    col += 1;
    y_move += 1;
    Serial.print("right\n");
  }else if (y_dir >800 and y_move > 0 and maze[row][col-1] == false){
    col -= 1;
    y_move -= 1;
    Serial.print("left\n");
  }

  Serial.print("X-axis, Y-axis: ");
  Serial.print(x_dir);
  Serial.print(',');
  Serial.print(y_dir);
  Serial.print("\n\n");

  Serial.print(digitalRead(SW_pin));
  //blink the dot we want to move so it's easy to distinguish
  lc.setLed(0, x_move, y_move, true);
  delay(100);
  lc.setLed(0, x_move, y_move, false);
  delay(150);
  
}

Schematics

The schematics and wiring diagrams were generated using Fritzing.

Maze wiring.png Maze schematics.png

Results

Below is a video demonstrating how it works


References

How to move a dot on the 8x8 LED Matrix using a joystick

How to connect a DC motor and control it from the sketch

How to use a joystick on the Arduino

Arduino LED Matrix Tutorial

LedControl External Library