Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Donato Brusamento
Published © GPL3+

M5Stack Christmas Lego Robot Santa Helper

Based on a LEGO NXT Mindstorm base for movement, and a M5Stack Grey Core for brains. Controlled by a simple web interface

BeginnerWork in progress4 hours2,309

Things used in this project

Hardware components

Mindstorms NXT Programming Brick / Kit
LEGO Mindstorms NXT Programming Brick / Kit
×1
ESP32 GREY Development Kit with 9Axis Sensor
M5Stack ESP32 GREY Development Kit with 9Axis Sensor
×1
M5Stack BaseX EV3 motor compatible base RJ11 interface
×1
ATtiny85
Microchip ATtiny85
×1
Breadboard (generic)
Breadboard (generic)
×1
Through Hole Resistor, 82 kohm
Through Hole Resistor, 82 kohm
×2
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

PlatformIO IDE
PlatformIO IDE
RobotC
Arduino IDE
Arduino IDE

Story

Read more

Schematics

MessageBox schematic (I2C buffer)

I2C pullup resistor for NXT interface

courtesy of Dexter Industries

Code

Attiny MessageBox code

Arduino
Using SpenceKonde's AttinyCore
#include <Wire.h>


const byte whoami = 0x85;
volatile bool byteIsCommand = true;

volatile bool motorCommandsIn[3] = {};
volatile bool motorCommandsOut[3] = {};

volatile bool sensorDataIn[3] = {};
volatile bool sensorDataOut[3] = {};

volatile bool data2send = false;
volatile bool data2store = false;

volatile uint8_t dataIn[3];
volatile uint8_t dataOut[3];

typedef enum {
  REQUEST_ID = 0x01,
  SWITCH_MASTER,
  REQUEST_SENSOR_DATA,
  REQUEST_MOTOR_DATA,
  WRITE_MOTOR_DATA,
  WRITE_SENSOR_DATA,
  LIGHT_LED = 0x80
} CommunicationType;

volatile CommunicationType command;
volatile bool ledActive = false;

void setup() {
    digitalWrite(SCL,LOW);
  digitalWrite(SDA,LOW);

  Wire.begin(0x34);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  pinMode(4, OUTPUT);


}

void loop() {

  if (data2store) {
    data2store = false;
    for (int i = 0; i < 3; i++) {
      if (command == WRITE_MOTOR_DATA) {
        motorCommandsIn[i] = dataIn[i];
        motorCommandsOut[i] = dataIn[i];
      }
      else if (command == WRITE_SENSOR_DATA) {
        sensorDataIn[i] = dataIn[i];
        sensorDataOut[i] = dataIn[i];
      }
    }
  }

  if (data2send) {
    data2send = false;
    for (int i = 0; i < 3; i++) dataOut[i] = 0x00;
    switch (command) {
      
      case REQUEST_ID:
        dataOut[1] = 0x85;
        break;
      
      case REQUEST_MOTOR_DATA:
        for (int i = 0; i < 3; i++) dataOut[i] = motorCommandsOut[i];
        break;
      
      case REQUEST_SENSOR_DATA:
        for (int i = 0; i < 3; i++) dataOut[i] = sensorDataOut[i];
              break;
      
      default:
        dataOut[1] = 0x3f;
        break;
    }
  }

  if (ledActive) digitalWrite(4, !digitalRead(4));
  delay(500);
}

void receiveEvent(int nBytes) {
  if (Wire.available() > 0) {
    byte data = Wire.read();
    command = (CommunicationType)data;
  }
  // store next 3 bytes of data
  if (command == WRITE_MOTOR_DATA || command == WRITE_SENSOR_DATA) {
    data2store = true;
    int idx = 0;
    while (Wire.available() > 0 && idx < 3) {
      dataIn[idx] = Wire.read();
    }
  }
  else if (command == LIGHT_LED) {
    ledActive = true;
    digitalWrite(4, HIGH);
  }
  else if (command== REQUEST_ID || command == REQUEST_SENSOR_DATA || command == REQUEST_MOTOR_DATA) {
    data2send = true;
  }
}

void requestEvent() {
  Wire.write(dataOut, 3);
}

Credits

Donato Brusamento

Donato Brusamento

1 project • 0 followers

Comments