Categories

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

Adding support for ESP32 boards in the Arduino IDE

This is  the github instructions, I have been looking at the ESP32 module

My ESP Arduino installation is located at J:\esp on my PC but other than changing the paths everything went as planned. The board I bought was a Lolin32 from Wemos – we’ll come to that later

Tested on 64 bit Windows 10 machine (I don’t have 32 Bit)

  1. Download and install the latest Arduino IDE Windows Installer from arduino.cc
  2. Download and install Git from git-scm.com
  3. Start Git GUI and run through the following steps:
    • Select Clone Existing Repository
      Step 1
    • Select source and destination
      • Source Location: https://github.com/espressif/arduino-esp32.git
      • Target Directory: C:/Users/[YOUR_USER_NAME]/Documents/Arduino/hardware/espressif/esp32
      • Change this to your Sketchbook Location if you have a different directory listed underneath the “Sketchbook location” in Arduino preferences.
      • Click Clone to start cloning the repository
      • Step 2 Step 3
    • Open C:/Users/[YOUR_USER_NAME]/Documents/Arduino/hardware/espressif/esp32/tools and double-click get.exeStep 4
    • When get.exe finishes, you should see the following files in the directoryStep 5
  4. Plug your ESP32 board and wait for the drivers to install (or install manually any that might be required)
  5. Start Arduino IDE
  6. Select your board in Tools > Board menu
  7. Select the COM port that the board is attached to
  8. Compile and upload (You might need to hold the boot button while uploading)Arduino IDE Example

How to update to the latest code

 

  1. Start Git GUI and you should see the repository under Open Recent Repository. Click on it!
  2. Update Step 1

 

  1. From menu Remote select Fetch from > origin
  2. Update Step 2
  3. Wait for git to pull any changes and close Git GUI
  4. Open C:/Users/[YOUR_USER_NAME]/Documents/Arduino/hardware/espressif/esp32/tools and double-click get.exeStep 4