Skip to content

Instantly share code, notes, and snippets.

@erikaheidi
Last active May 1, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikaheidi/be056e63cc5a503d22d97c1258832367 to your computer and use it in GitHub Desktop.
Save erikaheidi/be056e63cc5a503d22d97c1258832367 to your computer and use it in GitHub Desktop.

Arduino Tutorials

This is an example of PHP code to send commands via serial, as shown in the talk:

<?php
require __DIR__ . '/vendor/autoload.php';
$serial = new PhpSerial();
$serial->deviceSet("/dev/ttyUSB0");
$serial->confBaudRate(19200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
echo "Initializing device...";
sleep(5); // long sleep guarantees the device is initialized before sending messages.
echo "Now sending message...";
$serial->sendMessage("PHP and Arduino\n");
sleep(2);
$serial->deviceClose();

On the Arduino side, you'll need make sure you're using the same BAUD RATE for reading the commands. This is the Arduino code I use to read the commands from my LEDBOX:

#include "Charliplexing.h"
#include "Myfont.h"
#include "Arduino.h"

const int maxChars = 280;
const char endMarker = '\n';

unsigned char displayText[maxChars];
unsigned char rcvText[maxChars];
unsigned char defaultText[] = "LolBox v0.1 \0";
unsigned int state = 0;
unsigned int indexRcv = 0;

void setup()
{
  // initialize serial:
  Serial.begin(19200);
  Serial.setTimeout(5000);
  LedSign::Init();
  //Serial.println("<Arduino is ready>");
  strcpy(displayText, defaultText); // sets default text
}

void loop()
{
    char rc;
    
    if(Serial.available()) {
        if(!state) { //new transmission, reset stuff
            state = 1; // state = started
            indexRcv = 0;
            strcpy(rcvText,"");
            //Serial.println("Reading transmission...");
        }
        
        while(Serial.available() > 0) {
            rc = Serial.read();

            if(rc != endMarker) {
                rcvText[indexRcv] = rc;
                indexRcv++; 
            } else {
                rcvText[indexRcv] = '\0';
                state = 2; // state = finished
                //Serial.println("Finished reading");
            }
        }
    }

    if (state == 2) { // do what you gotta do
        //Serial.println("State Finished, now copying string");
        state = 0;
        strcpy(displayText, rcvText);
    }
    
    Myfont::Banner(strlen(displayText), displayText);
}

More resources on serial communication on Arduino:

Raspberry Pi Tutorials

This is how the PHP code to blink an LED on the Raspberry Pi looks like:

<?php
require 'vendor/autoload.php';
use PiPHP\GPIO\GPIO;
use PiPHP\GPIO\Pin\PinInterface;
$gpio = new GPIO();
$pin = $gpio->getOutputPin(19);
while (true) {
  $pin->setValue(PinInterface::VALUE_HIGH);
  sleep(1);
  $pin->setValue(PinInterface::VALUE_LOW);
  sleep(1);
}

OpenScad

Example code: generating a nametag

font = "Ubuntu Mono";
letter_size = 60;
padding = 20;

string = "@erikaheidi";
textlen = len(string);

box_width = letter_size*textlen*0.8;
box_height = letter_size + (2*padding);
box_thickness = 20;

start_x = 0 - (box_width / 2) + padding;
start_z = padding;

module text3d(string) {
  linear_extrude(height = box_thickness - 10) {
    text(string, size = letter_size, font = font, halign = "center", valign = "center", $fn = 64);
  }
}

module tag(width, height, thickness) {
    spacing = 20;
    difference() {
        //tag body
        linear_extrude(thickness) {
            square([width, height], center = true);
        }
        //tag hole for lanyard
        linear_extrude(thickness) {
            hole_x = 0 - width / 2 + spacing;
            hole_y = 0 - height / 2 + spacing;
            translate([hole_x, hole_y, 0]) square([15, height-spacing*2]);
        }
    }
}

difference() {
    tag(box_width, box_height, box_thickness);
    translate([0+padding, 0, start_z]) text3d(string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment