Golang and Arduino

Small arduino and golang programs

arduino blink without delay when button is pressed


// led pin 
const int ledPin1 = 13;
// button pin const int buttonPin1 = 2;
// blinking delay const int interval1 = 500; bool ledState1; bool canBlink1; unsigned long previousMillis1; void setup() { pinMode(ledPin1, OUTPUT); // set led pin to output pinMode(2, INPUT_PULLUP); // button pin set to INPUT with internal PULLUP(default state will be HIGH) } void loop() { unsigned long currentMillis = millis(); bool buttonState1 = digitalRead(buttonPin1); if (buttonState1 == LOW) { while (digitalRead(buttonPin1) == LOW); if (digitalRead(buttonPin1) == HIGH) { canBlink1 = !canBlink1; digitalWrite(ledPin1, LOW); } } if (currentMillis - previousMillis1 >= interval1 && canBlink1) { previousMillis1 = currentMillis; ledState1 = !ledState1; digitalWrite(ledPin1, ledState1); } }

arduino split integer to several numbers from 0 to 9


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

unsigned int number;
void loop() {
  // put your main code here, to run repeatedly:

  unsigned int dec[4] = {0};
  unsigned int value = number;
  value = value % 10000;
  dec[3] = value / 1000;
  value = value % 1000;
  dec[2] = value / 100;
  value = value % 100;
  dec[1] = value / 10;
  dec[0] = value % 10;

  number++;
  char buff[128];
  sprintf(buff, "%d = %d %d %d %d", number, dec[3], dec[2], dec[1], dec[0]);
  Serial.println(buff);
  delay(100);

}

arduino control 7 seg using arrays

simple programa to print number to 7 digits display

const int a = 2;
const int b = 3;
const int c = 4;
const int d = 5;
const int e = 6;
const int f = 7;
const int g = 8
const int h = 9;

int pins[8] = { a, b, c, d, e, f, g, h };
int numbers[10][8] = {
//  a  b  c  d  e  f  g  h
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 0 pins 1 = HIGH and 0 = LOW
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 1 pins
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 2 pins
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 3 pins
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 4 pins
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 5 pins
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 6 pins
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 7 pins
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 8 pins
  { 1, 0, 0, 0, 0, 0, 0, 0 }, // 9 pins
};

void display(int number) {
  for (int i = 0; i < 8; i++) {
    digitalWrite(pins[i], numbers[number][i]);
  }
}

void setup() {
  // put your setup code here, to run once:
  display(3);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Control relay with Raspberry PI in python in web

Control relay via web on raspberry Pi

First you need to install some dependencies

apt-get install python-rpi.gpio python3-rpi.gpio python-flask python3-flask

after that, create file: app.py and paste following code to it. edit MYRELAY to your pin ( in my example i am using 8 physical pin ) 


from flask import Flask
import RPi.GPIO as GPIO

MYRELAY = 8

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(MYRELAY, GPIO.OUT, initial=GPIO.LOW)

app = Flask(__name__)

@app.route("/on")
def turnon():
    GPIO.output(MYRELAY, GPIO.HIGH)
    return "OK"

@app.route("/off")
def turnoff():
    GPIO.output(MYRELAY, GPIO.LOW)
    return "OK"

if __name__ == "__main__":
    app.run(host="0.0.0.0",port='8080')

run

python3 app.py

now, ping your Raspberry Pi via http://ip:8080/on to turn Relay ON and http://ip:8080/off to turn relay off

Make a simple scheduler for arduino

After some googling, i decided to try to write and arduino library to simplify use of "millis" trick to schedule jobs with 'clean api'.

Here is some code of blink:


#include "SimpleScheduler.h"

void ledOn();
void ledOff();

void ledOn() {
  digitalWrite(LED_BUILTIN, HIGH);
  schedule(500, ledOff);
}
void ledOff() {
  digitalWrite(LED_BUILTIN, LOW);
  schedule(500, ledOn);
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  ledOn();
}

void loop() {
  scheduler_run();
}

you can find library at github arduino SimpleScheduler

arduino parse hex colors to numbers


void setup() {
    Serial.begin(9600);

    // get data from Serial.readStringUntil('\n'); to memory
    String data="FFFFFFFF 00FFFFFF 0000FFFF 000000FF";
    // store length of data
    int iend = data.length() - 1; 
    // current begin index
    int index=0;
    // next index
    int next_index;
    do{
        next_index=data.indexOf(' ',index);

        String hexcode=data.substring(index, next_index);

        index=next_index+1;

        unsigned long bigNumber = (unsigned long)strtoul(hexcode.c_str(), 0, 16);

        // add number to array

        Serial.println(bigNumber);

     }while((next_index!=-1)&&(next_index<iend));
}

void loop() {

}