Golang and Arduino

Small arduino and golang programs

arduino async led fader

const int ledPin = 13;

int direction = 10;
bool needToFade = true;
int fadeValue = 0;

void fader();

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

void loop() {
  static uint32_t prevMillis = 0;
  if(millis() - prevMillis > 100 && needToFade) {
    prevMillis = millis();
    fader();  
  }
}

void fader() {
  if(fadeValue > 255 || fadeValue < 0) {
    direction *= -1;
  }
  analogWrite(ledPin, fadeValue);
  fadeValue += direction;
  Serial.print("Debug fade: ");
  Serial.println(fadeValue);
}

arduino state machine with millis instead of delay

#include <Arduino.h>

enum class State :uint8_t {
  noop,
  wait,
  state1,
  state2,
  state3
};

State nextState;
State state;

uint32_t prevTime = millis();
uint32_t currentTime = millis();
uint32_t waitDelay;

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

void nextStateAfter(State ss, uint32_t d) {
  waitDelay = d;
  nextState = ss;
  state = State::wait;
  prevTime = currentTime;
}

void loop() {
  currentTime = millis();

  switch (state) {
    case State::noop:
      //maybe wait for input? (for now just send anything to serial);
      if (Serial.available() > 0) {
        Serial.readString();
        state = State::state1;
      }
      break;
    case State::wait:
      if (currentTime - prevTime > waitDelay) {
        state = nextState;
        prevTime = currentTime;
      }
      break;
    case State::state1:
      nextStateAfter(State::state2, 1000);
      Serial.println("state1");
      break;

    case State::state2:
      nextStateAfter(State::state3, 2000);
      Serial.println("state2");
      break;

    case State::state3:
      nextStateAfter(State::state1, 3000);
      Serial.println("state3");
      break;

  }
}

laravel php array to dot notation

<?php

namespace App\Support;

use Illuminate\Support\Facades\Log;

class HtmlToString {
        private static $return = [];

        public static function parse($input) {
                parse_str($input, $output);
                self::$return = [];
                self::_parse($output);
                return implode(".", self::$return);
        }

        private static function _parse($item, $depth = 0)
        {
                $key = key($item);
                self::$return[] =  $key;
                if(is_array($item[$key])) {
                        self::_parse($item[$key], $depth+1);
                }

        }
}

usage:

echo HtmlToString::parse("form[field][field2]");



results:

form.field.field2

arduino blink without delay new way


typedef void (*myFunction)() ;

class RepeatEvery {
  private:
    unsigned long currentMillis = millis();
    unsigned long prevMillis = currentMillis;
    bool prevCanRun = false;
    unsigned long lwait;
    myFunction lf;
  public:
    RepeatEvery(myFunction f, unsigned long wait) {
      lwait = wait;
      lf = f;
    }

    void handle() {
      bool r = true;
      handle(r);
    }

    void handle(bool& canRun, bool reset = true) {
      currentMillis = millis();
      if (prevCanRun != canRun) {
        prevCanRun = canRun;
        if (reset == true)
          prevMillis = currentMillis;
        else
          prevMillis = currentMillis - lwait - 1;
      }
      if (currentMillis - prevMillis >= lwait && prevCanRun == true ) {
        prevMillis = currentMillis;
        lf();
      }
    }
};

bool runOnce = true;
bool canRun = true;

// function which depends on external variable
// blink LED_BUILTIN sveral times
RepeatEvery fn([] {
  Serial.println("Blink Blink");
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}, 1000);

// function which turns on or of function named "fn"
// disables blinking after 5s and sets LED_BUILDIN to LOW
RepeatEvery repeatAlways([] {
  runOnce = false; // stop repeatAlways
  canRun = false; // stop fn
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("disable blinking");
}, 5000);

void setup() {

  Serial.begin(9600);
  while (!Serial);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  repeatAlways.handle(runOnce);
  fn.handle(canRun, true);
}

arduino debounce any pin function

P.S. all buttons should be connected to ground without any resistors.


bool debounce(const int pin) {
  static unsigned long ldt = 0;
  static unsigned long dbdelay = 50;

  static bool lbs = HIGH;
  static bool bs = LOW;

  const bool input = digitalRead(pin);

  if (input != lbs) {
    ldt = millis();
  }

  if (millis() - ldt > dbdelay) {
    if (input != bs) {
      bs = input;
      if (input == LOW) {
        return true;
      }
    }
  }
  lbs = input;
  return false;
}

const uint8_t inputPins[] { 8 } ; // just add more pins like { 8, 2, 3, 4 };

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

  // set input pins to INPUT_PULLUP mode
  for (uint8_t i; i < sizeof(inputPins) / sizeof(inputPins[[0]); i++) {
  pinMode(inputPins[i], INPUT_PULLUP);
  }
}

void loop() {
  // check if button on pin 8 is connected to ground (pressed)
  if (debounce(8)) {
    Serial.println("hello");
  }
}

arduino extract time from epoch / unixtime


unsigned long unixtime;
void setup() { Serial.begin(9600); } unsigned long timeNow; void loop() { if(millis() - timeNow >= 1000) { // every second increment unixtime timeNow = millis(); unixtime += 1; sprint(); } } void sprint() { int s = unixtime % 60; int m = (unixtime % 3600) / 60; int h = (unixtime % 86400) / 3600; char buff[64]; snprintf(buff, 64, "%02d %02d %02d", h, m, s); Serial.println(buff); }