Components and supplies
Ultrasonic Sensor - HC-SR04 (Generic)
OLED Display, Blue on Black
Arduino Nano R3
Rotary Encoder with Push-Button
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Parking Assist Stoplight code
arduino
Code for the stoplight, adjust the light distance ranges to your liking!
1/* 2 * Parking Assist Stoplight 3 * Created by Ty Palowski 4 * https://youtube.com/c/typalowski 5 */ 6 7//For Rotary Encoder 8int inputCLK = 4; //Encoder Pin CLK (Clock) 9int inputDT = 2; //Encoder Pin DT (Data) 10int inputSW = 8; //Encoder Pin SW (Switch) 11 12int counter = 0; 13int currentStateCLK = LOW; 14int previousStateCLK = currentStateCLK; 15int switchState = HIGH; 16 17String encdir =""; 18 19//For Ultrasonic Sensor 20#define trigPin 9 21#define echoPin 10 22 23#define led1 12 24#define led2 11 25#define led3 13 26 27int light1; 28int light2; 29int light3; 30int distance1; 31 32int const light1s = 12; 33int const light2s = 24; 34int const light3s = 48; 35 36//For OLED Display 37#include <SPI.h> 38#include <Wire.h> 39#include <Adafruit_GFX.h> 40#include <Adafruit_SSD1306.h> 41 42#define SCREEN_WIDTH 128 // OLED display width, in pixels 43#define SCREEN_HEIGHT 32 // OLED display height, in pixels 44 45// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 46#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) 47 48Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 49 50//#define LOGO_HEIGHT 16 51//#define LOGO_WIDTH 16 52const unsigned char myBitmap [] PROGMEM = 53{ // 'stoplight and jeep, 128x32px 54 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 55 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 56 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x01, 0x80, 0x06, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 57 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x01, 0x80, 0x06, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 58 0x00, 0xff, 0xfc, 0xff, 0xfc, 0x00, 0x00, 0x01, 0xe0, 0x07, 0x80, 0x1c, 0x00, 0x00, 0x00, 0x00, 59 0x00, 0xff, 0xf0, 0x3f, 0xfc, 0x00, 0x07, 0xc1, 0xe0, 0x07, 0x80, 0x0e, 0x00, 0x00, 0x00, 0x00, 60 0x00, 0xff, 0xf0, 0x1f, 0xfc, 0x00, 0x07, 0xc1, 0xe0, 0x07, 0x80, 0x1e, 0x00, 0x00, 0x00, 0x00, 61 0x00, 0x7f, 0xf0, 0x1f, 0xf8, 0x00, 0x06, 0xc1, 0xe0, 0x07, 0x80, 0x3e, 0x00, 0x00, 0x00, 0x00, 62 0x00, 0x3f, 0xf0, 0x1f, 0xf0, 0x00, 0x07, 0xc1, 0xb0, 0x06, 0xc0, 0x3f, 0xff, 0x00, 0x00, 0x00, 63 0x00, 0x0f, 0xf0, 0x3f, 0xe0, 0x00, 0x07, 0xcf, 0xf8, 0x07, 0xc0, 0x3f, 0xff, 0xff, 0x80, 0x00, 64 0x00, 0x0f, 0xf8, 0x7f, 0xc0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 65 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x07, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xa0, 0x00, 66 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0x07, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 67 0x00, 0xff, 0xfe, 0xff, 0xfc, 0x00, 0x07, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 68 0x00, 0xff, 0xf0, 0x3f, 0xfc, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 69 0x00, 0xff, 0xf0, 0x1f, 0xfc, 0x00, 0x07, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 70 0x00, 0x7f, 0xf0, 0x1f, 0xf8, 0x00, 0x07, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 71 0x00, 0x3f, 0xf0, 0x1f, 0xf8, 0x00, 0x07, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 72 0x00, 0x1f, 0xf0, 0x3f, 0xe0, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 73 0x00, 0x0f, 0xf8, 0x3f, 0xc0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 74 0x00, 0x07, 0xff, 0xff, 0xc0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 75 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 76 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 77 0x00, 0xff, 0xf8, 0x7f, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xf6, 0x00, 0x00, 0x6f, 0xff, 0xc0, 0x00, 78 0x00, 0xff, 0xf0, 0x3f, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xf0, 0x00, 0x00, 0x07, 0xff, 0xc0, 0x00, 79 0x00, 0xff, 0xf0, 0x1f, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xf0, 0x00, 0x00, 0x0f, 0xff, 0xc0, 0x00, 80 0x00, 0x7f, 0xe0, 0x1f, 0xf8, 0x00, 0x00, 0x01, 0xff, 0xe0, 0x00, 0x00, 0x07, 0xff, 0x80, 0x00, 81 0x00, 0x3f, 0xf0, 0x1f, 0xf0, 0x00, 0x00, 0x01, 0xff, 0xe0, 0x00, 0x00, 0x07, 0xff, 0x80, 0x00, 82 0x00, 0x0f, 0xf0, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x00, 0x00, 0x03, 0xff, 0x00, 0x00, 83 0x00, 0x0f, 0xfc, 0x7f, 0xc0, 0x00, 0x00, 0x00, 0x7f, 0x80, 0x00, 0x00, 0x01, 0xfe, 0x00, 0x00, 84 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 85 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 86 87void setup() 88{ Serial.begin (9600); 89 pinMode(trigPin, OUTPUT); 90 pinMode(echoPin, INPUT); 91 pinMode(led1, OUTPUT); 92 pinMode(led2, OUTPUT); 93 pinMode(led3, OUTPUT); 94 95attachInterrupt(digitalPinToInterrupt(inputDT), update, CHANGE); 96 97 // Set encoder pins as inputs 98 pinMode (inputCLK, INPUT); 99 pinMode (inputDT, INPUT); 100 pinMode(inputSW, INPUT_PULLUP); 101 102 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally 103 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 104 Serial.println(F("SSD1306 allocation failed")); 105 for(;;); // Don't proceed, loop forever 106 } 107 108 // Clear the buffer 109 display.clearDisplay(); 110 display.setRotation (2); 111 112 display.drawBitmap(0, 0, myBitmap, 128, 64, WHITE); // display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color) 113 114 // Show the display buffer on the screen. You MUST call display() after 115 // drawing commands to make them visible on screen! 116 display.display(); 117 delay(2000); 118} 119 120void loop(){ 121// BUTTON 122 switchState = digitalRead(inputSW); // Read the digital value of the switch (LOW/HIGH) 123 // If the switch is pressed (LOW), print message 124if (switchState == LOW) { 125 Serial.println("Switch pressed"); 126 counter = distance1 - light1s; 127 } 128 129 long duration, distance; 130 digitalWrite(trigPin, LOW); 131 delayMicroseconds(2); //2 132 digitalWrite(trigPin, HIGH); 133 delayMicroseconds(10); 134 digitalWrite(trigPin, LOW); 135 duration = pulseIn(echoPin, HIGH); 136 distance = (duration/2) / 73.914; //29.1(cm) 137 light1 = counter + light1s; 138 light2 = light1 + light2s; 139 light3 = light2 + light3s; 140 distance1 = distance; 141 142 143if (distance < light1) 144 { //RED LIGHT 145 digitalWrite(led1,HIGH); 146 digitalWrite(led2,LOW); 147 digitalWrite(led3,LOW); 148 } 149else if (distance > light1 && distance < light2) 150 { //YELLOW LIGHT 151 digitalWrite(led2,HIGH); 152 digitalWrite(led1,LOW); 153 digitalWrite(led3,LOW); 154 } 155else if (distance > light2 && distance < light3) 156 { //GREEN LIGHT 157 digitalWrite(led3,HIGH); 158 digitalWrite(led1,LOW); 159 digitalWrite(led2,LOW); 160 } 161else 162 { 163 digitalWrite(led1,LOW); 164 digitalWrite(led2,LOW); 165 digitalWrite(led3,LOW); 166 } 167 displayStopDist(); 168 display.display(); 169} 170 171void update() { 172 // Read the current state of inputCLK 173 currentStateCLK = digitalRead(inputCLK); 174 175 if ((previousStateCLK == LOW) && (currentStateCLK == HIGH)) { 176 if (digitalRead(inputDT) == HIGH) { 177 counter --; 178 } 179 else { 180 counter ++; 181 } 182 } 183 previousStateCLK = currentStateCLK; 184} 185 186void displayStopDist(){ 187 188 // Define d as Stop Distance 189 float d = light1; 190 float c = distance1; 191 // Clear the display 192 display.clearDisplay(); 193 //Set the color - always use white despite actual display color 194 display.setTextColor(WHITE); 195 //Set the font size 196 display.setTextSize(1); 197 //Set the cursor coordinates 198 display.setCursor(0,0); 199 display.print("Parking Stop Assist"); 200 display.setCursor(0,11); 201 display.print("Stop Dist: "); 202 display.setTextSize(2); 203 display.print(d,0); 204 display.print("in"); 205 display.setTextSize(1); 206 display.setCursor(0,25); 207 //display.print("Temperature: "); 208 display.print(c,0); 209 display.print("in"); 210}
Downloadable files
Wiring Diagram for Stoplight Parking Assist Project
The resistor is 330ohms!
Wiring Diagram for Stoplight Parking Assist Project
Wiring Diagram for Stoplight Parking Assist Project
The resistor is 330ohms!
Wiring Diagram for Stoplight Parking Assist Project
Documentation
Stoplight Shades
3D model
Stoplight Shades
Stoplight Base housing
3D Model
Stoplight Base housing
Stoplight Shades
3D model
Stoplight Shades
Stoplight Base housing
3D Model
Stoplight Base housing
Comments
Only logged in users can leave comments
typalowski
0 Followers
•0 Projects
Table of contents
Intro
17
0