Categories

Arduino traffic light example

This was a a simple board that I bought which basically consists of a red, yellow and green LED on a board, rather than buying seperate components and using a breadboard this is a reasonably low cost solution.

 

Code

In the example we use Pin 5 , 6 and 7 for the green, yellow and red LEDs

 

byte pinLedRed = 7;
byte pinLedYellow = 6;
byte pinLedGreen = 5;
 
void setup()
{
    // Initialize the pins as output pins
    pinMode(pinLedRed, OUTPUT);
    pinMode(pinLedYellow, OUTPUT);
    pinMode(pinLedGreen, OUTPUT);
}
 
void loop()
{
 
    //Red signal
    digitalWrite(pinLedRed, HIGH); // RED on
    delay(5000); // 5 secs
 
    //Yellow + Red
    digitalWrite(pinLedYellow, HIGH); // YELLOW on
    delay(1000); // 1 sec
 
    //Green signal
    digitalWrite(pinLedRed, LOW); // RED  off
    digitalWrite(pinLedYellow, LOW); // YELLOW off
    digitalWrite(pinLedGreen, HIGH); // GREEN  on
    delay(5000); // 5 secs
 
    //Yellow signal
    digitalWrite(pinLedGreen, LOW); // GREEN  off
    digitalWrite(pinLedYellow, HIGH); // YELLOW  on
    delay(1000);
    digitalWrite(pinLedYellow, LOW); // YELLOW  off
 
}

 

Link

The traffic light board only costs about $1

LED traffic lights light-emitting module / digital signal output Traffic light module / electronic building blocks

Roll the dice codebug example

A simple dice game on a codebug

Go to http://www.codebug.org.uk/create/ and paste the code in, the good thing is this also generates a Python and blockly example

Code

var delay;
var pressed;
var count;
 
 
codebug_direction('U');
codebug_sleepafter(1);
io_configure(0, IO_DIGITAL_INPUT);
io_configure(1, IO_DIGITAL_INPUT);
io_configure(2, IO_DIGITAL_INPUT);
io_configure(3, IO_DIGITAL_INPUT);
io_configure(4, IO_DIGITAL_INPUT);
io_configure(5, IO_DIGITAL_INPUT);
io_configure(6, IO_DIGITAL_INPUT);
io_configure(7, IO_DIGITAL_INPUT);
io_configure_pullup(0, 0);
io_configure_pullup(2, 0);
io_configure_pullup(3, 0);
io_configure_pullup(4, 0);
io_configure_pullup(5, 0);
 
 
delay = 60;
pressed = 0;
count = 1;
while (true) {
  if (io_get_input('A') == 1) {
    pressed = 1;
    fivebyfivedisplay.clear();
    fivebyfivedisplay.update();
    while (io_get_input('A') == 1) {
      count = count + 1;
    }
    count = count % 6;
  }
  if (1 == pressed) {
    fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]]));
    fivebyfivedisplay.update();
    sleep(delay);
    fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 1, 1, 1]]));
    fivebyfivedisplay.update();
    sleep(delay);
    fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 1, 1, 1, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]]));
    fivebyfivedisplay.update();
    sleep(delay);
    fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 1, 0], [0, 1, 0, 0, 1], [1, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0]]));
    fivebyfivedisplay.update();
    sleep(delay);
    fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]));
    fivebyfivedisplay.update();
    sleep(delay);
    fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]]));
    fivebyfivedisplay.update();
    sleep(delay);
    fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0]]));
    fivebyfivedisplay.update();
    sleep(delay);
    fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]));
    fivebyfivedisplay.update();
    sleep(delay);
    if (0 == count) {
      count = 6;
    }
    if (1 == count) {
      fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]));
      fivebyfivedisplay.update();
    } else if (2 == count) {
      fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 0]]));
      fivebyfivedisplay.update();
    } else if (3 == count) {
      fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]]));
      fivebyfivedisplay.update();
    } else if (4 == count) {
      fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1]]));
      fivebyfivedisplay.update();
    } else if (5 == count) {
      fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1]]));
      fivebyfivedisplay.update();
    } else if (6 == count) {
      fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1]]));
      fivebyfivedisplay.update();
    }
    pressed = 0;
  }
}

 

To test this out , simply upload the example – following the instructions on the website. Press the A button to roll the dice

ESP8266 AT Instruction Set

esp32 technical reference manual

A basic ESP32 Web server example

In this example we will create a basic web server with an ESP32, we will then serve a we page with an on and off button which will switch an LED on and off

Parts Required

1x ESP32 Dev Module (Lolin32)
1x LED
1x Breadboard
1x 470 Ohm Resistor
Jumper wires

Layout

 

Code

 

#include <WiFi.h>
 
// Replace with your network credentials
const char* ssid = "iainhendry";
const char* password = "iain061271";
 
WiFiServer server(80);
 
const int led = 15; // the number of the LED pin
 
// Client variables
char linebuf[80];
int charcount=0;
 
void setup()
{
// initialize the LED as an output:
pinMode(led, OUTPUT);
//Initialize serial and wait for port to open:
Serial.begin(115200);
while(!Serial) {
}
 
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
 
WiFi.begin(ssid, password);
 
// attempt to connect to Wifi network:
while(WiFi.status() != WL_CONNECTED)
{
// Connect to WPA/WPA2 network.
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
 
void loop()
{
// listen for incoming clients
WiFiClient client = server.available();
if (client)
{
Serial.println("New client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.write(c);
//read char by char HTTP request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
 
if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
client.println("<!DOCTYPE HTML><html><head>");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>");
client.println("<h1>ESP32 - Web Server example</h1>");
client.println("<p>LED <a href=\"on\"><button>ON</button></a>&nbsp;<a href=\"off\"><button>OFF</button></a></p>");
client.println("</html>");
break;
}
if (c == '\n')
{
// you're starting a new line
currentLineIsBlank = true;
if (strstr(linebuf,"GET /on") > 0)
{
Serial.println("LED ON");
digitalWrite(led, HIGH);
}
else if (strstr(linebuf,"GET /off") > 0)
{
Serial.println("LED OFF");
digitalWrite(led, LOW);
}
 
// you're starting a new line
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r')
{
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
 
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}

 

 

Testing

Open the serial monitor to get the assigned IP address, in your favourite web browser navigate to this IP address

This is my example

 

Now press the on and off button and check if the LED flashes

Links

You can pick up a module for about $8

Official DOIT ESP32 Development Board WiFi+Bluetooth Ultra-Low Power Consumption Dual Core ESP-32