Skip to content

Instantly share code, notes, and snippets.

@Macrofig
Last active June 18, 2025 22:15
Show Gist options
  • Save Macrofig/7b664e47f226281a04d51fe9a160c0b2 to your computer and use it in GitHub Desktop.
Save Macrofig/7b664e47f226281a04d51fe9a160c0b2 to your computer and use it in GitHub Desktop.
traffic light

Setup Arduino IDE

To get Arduino IDE to work with 8266 boards:

  • Add https://arduino.esp8266.com/stable/package_esp8266com_index.json to the Settings area, "Additional Boards" field.
  • Open Board Manager, and install the Generic 8266 board.

To get 6612 motor control library to work:

To get Wifi control to work:

Cheat Sheet

// Adding libraries
#include <Servo.h>

// Creating Variables
#define LED_PIN = 1;
const int SENSOR_PIN = 2;
Servo servoArm;

// Setting up Pins
pinMode(LED_PIN, OUTPUT);
servoArm.attach(SERVO_PIN);

// Using Pins
digitalRead(SENSOR_PIN);
digitalWrite(LED_PIN, HIGH);
myservo.write(100);

// Creating functions
void sayHello(String name) {
  Serial.println("HELLO!" + name);
}

// Calling functions
sayHello("Redwoods Wonder Forge");

// Controlling logic
if (isDone) {
  sayGoodbye();
} else {
  sayHello();
}
(isDone) ? sayGoodbye() : sayHello();

// Make the controller wait for a moment
delay(1000); // 1000 milliseconds is one second
#include <SparkFun_TB6612.h>
#define AIN1 2
#define AIN2 4
#define PWMA 5
#define BIN1 7
#define BIN2 8
#define PWMB 6
#define STBY 9
// these constants are used to allow you to make your motor configuration
// line up with function names like forward. Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;
// Initializing motors. The library will allow you to initialize as many
// motors as you have memory for. If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
void setup()
{
Serial.begin(9600);
}
void loop() {
brake(motor1, motor2);
delay(300);
back(motor1, motor2, 250);
delay(500);
left(motor1, motor2, 250);
delay(300);
forward(motor1, motor2, 150);
delay(500);
}
// Drive individual motors
// motor1.drive(255,1000);
// motor1.drive(-255,1000);
// motor1.brake();
// motor2.drive(255,1000);
// motor2.drive(-255,1000);
// motor2.brake();
// Forward with multiple motors
// forward(motor1, motor2, 150);
// Backward with multiple motors
// back(motor1, motor2, 50);
// Turn left
// left(motor1, motor2, 100);
// Turn right
// right(motor1, motor2, 100);
// Brake multiple motors
// brake(motor1, motor2);
// Add a package or library using the `include` directive
#include <Servo.h>
// Create some variables that your program will use but you might want to change from time to tim
#define LED_PIN = 1;
// You can also create a "Constant", a variable that cannot be changed while the program is running
const int SENSOR_PIN = 2;
const int SERVO_PIN = 4;
// You can create constants for anything you find useful, not just pin numbers.
const int SERVO_DIRECTION = 0; // 0 is counter clockwise
// Initialize a part of a library and set it as a variable
Servo servoArm;
// put your setup code here, to run once when the board boots up
void setup() {
// Create a "Serial" connection so you can receive messages about what your code is doing
Serial.begin(9600);
// You can begin sending messages to the logs
Serial.println(my_name());
// Set up your pins as you need them
pinMode(LED_PIN, OUTPUT); // Sends a signal TO the pin
pinMode(SENSOR_PIN, INPUT); // Waits for signals FROM the pin
// Some libraries have their own ways of using pins
servoArm.attach(SERVO_PIN);
}
// put your main code here, to run repeatedly
void loop() {
// Turn something on and off
digitalWrite(LED_PIN, HIGH); // HIGH means on
delay(1000); // 1000 milliseconds is one second
digitalWrite(LED_PIN, LOW); // LOW means off
// Use IF ELSE code to control behaviors of your creation!
if (SERVO_DIRECTION == 1) {
if (digitalRead(SENSOR_PIN) == 1) {
Serial.println('SENSOR DETECTED!')
}
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
Serial.println(pos);
myservo.write(pos);
delay(200);
}
} else {
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
Serial.println(pos);
myservo.write(pos);
delay(200);
}
}
}
// Create a custom function that you can use
// Note that the first word is what the function should return back to the caller - if nothing
// is returned use `void` instead.
String my_name() {
return "Redwoods Wonder Forge";
}
#include <Servo.h>
Servo servo;
void setup() {
servo.attach(2); //D4
servo.write(0);
delay(2000);
}
void loop() {
servo.write(90);
delay(1000);
servo.write(0);
delay(1000);
}
// This is the library for the TB6612 that contains the class Motor and all the
// functions
#include <SparkFun_TB6612.h>
// Pins for all inputs, keep in mind the PWM defines must be on PWM pins
// the default pins listed are the ones used on the Redbot (ROB-12097) with
// the exception of STBY which the Redbot controls with a physical switch
#define PWMA 16
#define AIN2 5
#define AIN1 4
#define BIN1 14
#define BIN2 2
#define PWMB 0
#define STBY 100
#define SNSRL 12
#define SNSRR 10
#define TRIG 15
#define ECHO 13
// these constants are used to allow you to make your motor configuration
// line up with function names like forward. Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;
const float speedOfSound = .0343;
const int maxDistance = 40;
// Initializing motors. The library will allow you to initialize as many
// motors as you have memory for. If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
void setup()
{
pinMode(TRIG,OUTPUT);
pinMode(ECHO,INPUT);
pinMode(SNSRL,INPUT);
pinMode(SNSRR,INPUT);
Serial.begin(9600);
}
float getDistance() {
digitalWrite(TRIG, LOW);
delay(2);
digitalWrite(TRIG, HIGH);
delay(10);
digitalWrite(TRIG, LOW);
return ( pulseIn(ECHO, HIGH) * speedOfSound) / 2;
}
void loop() {
Serial.println("distance");
float distance = getDistance();
Serial.println(distance);
int rightSensor = digitalRead(SNSRR);
int leftSensor = digitalRead(SNSRL);
Serial.println(leftSensor);
Serial.println(rightSensor);
// Found opponent
if (getDistance() < maxDistance && (leftSensor && rightSensor)) {
Serial.println("Attack!");
brake(motor1, motor2);
forward(motor1, motor2);
} else {
// Both sensors found edge or mat, back up quickly and turn around
if (!leftSensor && !rightSensor) {
Serial.println("DANGER!");
brake(motor1, motor2);
back(motor1, motor2, 250);
delay(800);
left(motor1, motor2, 250);
delay(400);
// push forward slowly
forward(motor1, motor2, 150);
}
// The left sensor found the edge, back up a bit and turn RIGHT quickly
if (!leftSensor && rightSensor) {
Serial.println("LEFT WARNING");
brake(motor1, motor2);
back(motor1, motor2, 250);
delay(200);
right(motor1, motor2, 250);
delay(300);
// push forward slowly and seek
forward(motor1, motor2, 150);
}
// The right sensor found the edge, back up a bit and turn LEFT quickly
if (leftSensor && !rightSensor) {
Serial.println("RIGHT WARNING");
brake(motor1, motor2);
back(motor1, motor2, 250);
delay(200);
left(motor1, motor2, 250);
delay(300);
// Push forward slowly and seek
forward(motor1, motor2, 150);
}
// Not at the edge, somewhere in the mat, seek for opponent
if (leftSensor && rightSensor) {
Serial.println("SEEKING...");
brake(motor1, motor2);
left(motor1, motor2, 250);
delay(600);
if (getDistance() < 40) {
forward(motor1, motor2, 150);
} else {
right(motor1, motor2, 250);
delay(200);
if (getDistance() < 40) {
forward(motor1, motor2, 150);
} else {
right(motor1, motor2, 250);
// Done checking, let the loop start up again and assume
// the initial check will take over
}
}
}
}
}
#define RED 5
#define YELLOW 4
#define GREEN 0
#define SENS 2
#define UP 14
int isUp = 0;
void setup()
{
pinMode(RED,OUTPUT);
pinMode(YELLOW,OUTPUT);
pinMode(GREEN,OUTPUT);
pinMode(UP,INPUT);
pinMode(SENS,INPUT);
Serial.begin(9600);
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(GREEN, LOW);
}
void setLightColor(String color) {
digitalWrite(RED, color == "RED" ? HIGH : LOW);
digitalWrite(YELLOW, color == "YELLOW" ? HIGH : LOW);
digitalWrite(GREEN, color == "GREEN" ? HIGH : LOW);
}
void loop() {
Serial.println("Traffic Light is ...");
// isUp = digitalRead(UP);
Serial.println(digitalRead(UP));
Serial.println(isUp == 1 ? "UP!" : "DOWN!");
if (isUp == 1) {
Serial.println("checking sensor");
Serial.print(digitalRead(SENS));
if (digitalRead(SENS) == 0) {
setLightColor("GREEN");
delay(5000);
setLightColor("YELLOW");
delay(2000);
setLightColor("RED");
}
} else {
Serial.println("Turning light OFF");
setLightColor("OFF");
delay(1000);
Serial.println("Turning light ON");
setLightColor("RED");
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment