Golang and Arduino

Small arduino and golang programs

arduino millis() trick with state machine

This sketch waits until button is pressed ( pin 2 connected with ground ) and if button was pressed -  blinks LED 3 times.

#include <Arduino.h>

const int LED_PIN = LED_BUILTIN;
const int BUTTON_PIN = 2;

const long blingTimes = 3;

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

  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

enum {
  turn_on,
  turn_off,
  wait,
};

byte state = wait;
unsigned long prevMillis1 = 0;
const unsigned long delay1 = 1000;
const unsigned long delay2 = 500;
long iteration = 0;

void loop() {

  unsigned long currentTime = millis();

  if(state == wait) {
    if(digitalRead(BUTTON_PIN) == LOW) {
      state = turn_on;
      iteration=0;
    }
  }

  if (state == turn_on) {
    if (millis() - prevMillis1 >= delay1) {
      prevMillis1 = currentTime;
      digitalWrite(LED_PIN, HIGH);
      state = turn_off;
    }
  }
  if (state == turn_off) {
    if (millis() - prevMillis1 >= delay2) {
      prevMillis1 = currentTime;
      digitalWrite(LED_PIN, LOW);
      state = turn_on;
      iteration++;
      if (iteration > blinkTimes) {
        state = wait;
      }
    }
  }
}

how to control several servo motors with arduino

The class


#include <Arduino.h>
#include <Servo.h>

class AsyncServo
{
  Servo servo;   
  int pos;       
  int increment; 
  int incrementBy = 1;
  int updateInterval = 10;       
  unsigned long lastUpdate; 
  bool done = false;

public:
  AsyncServo(int interval =10, int i = 1)
  {
    updateInterval = interval;
    increment = i;
    incrementBy = i;
  }

  // Attach pin to servo
  void Attach(int pin)
  {
    servo.attach(pin);
  }

  // Detach from servo
  void Detach()
  {
    servo.detach();
  }

  // Rotate servo to "Home" ( pos = 0);
  void Home()
  {
    if (pos >= 180)
    {
      done = false;
      increment = -incrementBy;
    }
  }

  // Rotate servo to "End" ( pos = 180 );
  void End()
  {
    if (pos <= 0)
    {
      done = false;
      increment = incrementBy;
    }
  }

  // just set servo to some position between 0 and 180;
  void setPosition(uint8_t p) {
    done = true;
    // pos = p; ????
    servo.write(pos);
  }

  // check if servo done its movement
  bool Done()
  {
    return done;
  }

  // check if servo is at home position: pos = 0;
  bool AtHome()
  {
    return pos <= 0;
  }

  // asynchroniuosly rotate motor
  void Run()
  {
    if (done)
      return;

    if ((millis() - lastUpdate) > updateInterval) 
    {
      lastUpdate = millis();
      pos += increment;
      servo.write(pos);
      Serial.println(pos);
      if ((pos >= 180) || (pos <= 0))
      {
        done = true;
        return;
      }
    }
  }
};

class usage on arduino UNO / mega ( for others - check for PWM pins )



AsyncServo s1(100, 10); // slow movement
AsyncServo s2(10, 10);  // fast movement

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

  s1.Home();
s2.Home();
}

void loop()
{
  s1.Run(); // servo 1 step
 s2.Run(); // servo 2 step 
  if (s1.Done()) // if servo motor stopped
  {
    if (s1.AtHome()) //check if its position is Home ( 0 ) 
    {
      s1.End(); // tell servo motor to rotate to max ( 180 )
    }
    else
    {
      s1.Home(); // tell servo motor to rate back to home ( 0 );
    }
  }
  if (s2.Done()) // if servo motor stopped
  {
    if (s2.AtHome()) //check if its position is Home ( 0 ) 
    {
      s2.End(); // tell servo motor to rotate to max ( 180 )
    }
    else
    {
      s1.Home(); // tell servo motor to rate back to home ( 0 );
    }
  }
} 

arduino updated eeprom: writeanything.h

#include <EEPROM.h>   // We need this library
#include <Arduino.h>  // for type definitions

//We create two fucntions for writing and reading data from the EEPROM
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          EEPROM.write(ee++, *p++);
    EEPROM.commit(); // comment out on some boards / older versions.
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
          *p++ = EEPROM.read(ee++);
    return i;
}

Usage


struct {
  char ssid[20];
  char passwd[20];
} wifiConfig;

void setup() {
  // write struct to eeprom
  EEPROM_writeAnything(0, wifiConfig);
  // read from eeprom to struct
  EEPROM_readAnything(0, wifiConfig);
}

Happy using ;)

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:

}