Skip to content

9. Machine building

The Mysa Automata

The Mysa Automata: the making off

Read our full documentation on our group page here.

Individual part: electronics

The electronics

For the electronics we basically need a microcontroller, a lot of buttons and switches, 5 servo motors and some connectors. Lots of soldering wires.

The initial plan is to use an ESP32 microcontroller. So we can also add wifi and a controller app to the machine. That is a load of extra work to make and program. We decided to take little steps and let it grow. So we started out using an arduino Nano with an extention shield. That enables us to more easily wire all buttons, leds and motors to the microcontroller. So we don’t need to make a custom board for this yet.

this expansion schield has 3pin servo headers for each arduino pin. So we have power and GND available for each pin we need.

Connecting all parts:

  • The leds in the big arcade buttons already have a limiting resistor build-in. So we don’t need to add an extra on to connect these to the arduino.

  • The buttons need some little extra circuit to have a consistent reading in de arduino. So we added a Pull-down resistor to al the wiring of the buttons.

  • the servos we can connect directly to the 3pin header on the arduinoshield. We do however have to be careful to connect them right, so we don’t damage the motors by connecting the 5V line to the signal line.

Time to code

We do the coding in the Arduino Ide, to start the testing. We first made code to light up 1 led and wait for the button that is lid to be pressed. When the button is pressed. We put out the Led and move the servo motor to lift the cookie. After waiting for 5secs we move the servo back to the starting position, to close the lid again. This cycle can be repeated over and over in de loop part of the code.

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
int pos = 140;    // variable to store the servo position

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 13;     // the number of the pushbutton pin, can be 7, 8, 12, 13
const int ledPin = 5;      // the number of the LED pin, can be 1, 2, 4, 5

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
boolean motor1Up = false;
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 300;           // interval to wait (milliseconds)

void setup() {
  myservo1.attach(10);  // servos can be pins 3, 6, 9, 10, 11
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  myservo1.write(-pos);
  Serial.begin(115200);
}

void loop() {
  // light up the led in the button
    digitalWrite(ledPin, HIGH);
  unsigned long currentMillis = millis();
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
       // turn LED off
      digitalWrite(ledPin, LOW);
      //switch motorstate
      motor1Up = !motor1Up;
    }
    //motor1Up = !motor1Up;


    Serial.println("btn high");
    Serial.println(motor1Up);

  } else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
    Serial.println(motor1Up);
    Serial.println("btn low");
  }
  if (motor1Up == true) {
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    digitalWrite(ledPin, LOW);
    delay(5000); //wait 5secs
    myservo1.write(-pos);
      motor1Up = !motor1Up;
  } else {
    myservo1.write(-pos);              // tell servo to go to position in variable 'pos'
    delay(15);
  }
}

We did some code adjustments and testing

Next part is to scale this up to the whole machine. In the back of the machine are 5 switches, where we can select where we put cookies in the machine. And so the arduino can know from what motors to choose a random one to give a cookie.

That part of the code is not completely finished. But for know it works to teach Mysa how to use the machine. We keep working on it to be able to make the exercises for Mysa and the teaching more interesting.

#include <Servo.h>

// selectorPins A0, A1, A2
// cookiePins A3, A4, A5, A6, A7
// buttonPins 7, 8, 12, 13
// ledPins 1, 2, 4, 5
// servos 3, 6, 9, 10, 11

int ledPins[] = {1, 2, 4, 5};
int servoPins[] = {3, 6, 9, 10, 11};
int buttonPins[] = {7, 8, 12, 13};

int cookiePins[] = {A1, A2, A3, A4, A5};
int hasCookie[] = {0, 0, 0, 0, 0};
int selectorPins[] = {A0, A6, A2};

int numServos = 5;
int numBtns = 4;

int prgSelect = 0;
int buttonState = 0;
int gameState = 0;
int giveCookie = 0;
int pos = 120;

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
Servo myservo3;  // create servo object to control a servo
Servo myservo4;  // create servo object to control a servo
Servo myservo5;  // create servo object to control a servo
Servo servoNames[] = {myservo1, myservo2, myservo3, myservo4, myservo5};

unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 300;           // interval to wait (milliseconds)
boolean motor1Up = false;

void readCookiePins() {
  for (int thisPin = 0; thisPin < numServos; thisPin++) {
    hasCookie[thisPin] = digitalRead(cookiePins[thisPin]);
  }
  for (int thisPin = 0; thisPin < numServos; thisPin++) {
    Serial.print("lift "); Serial.print(cookiePins[thisPin]); Serial.print(" ==> hascookie?  :  "); Serial.println(hasCookie[thisPin]);
  }
}
void Init() {
  readCookiePins();
  //setCookies();
  //whatButtons();
}


void setup() {
  // put your setup code here, to run once:
  for (int thisPin = 0; thisPin < numServos; thisPin++) {
    pinMode(cookiePins[thisPin], INPUT);
  }
  for (int thisPin = 0; thisPin < numBtns; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
  for (int thisPin = 0; thisPin < numBtns; thisPin++) {
    pinMode(buttonPins[thisPin], INPUT);
  }
  myservo1.attach(servoPins[0]);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(servoPins[1]);  // attaches the servo on pin 9 to the servo object
  myservo3.attach(servoPins[2]);  // attaches the servo on pin 9 to the servo object
  myservo4.attach(servoPins[3]);  // attaches the servo on pin 9 to the servo object
  myservo5.attach(servoPins[4]);  // attaches the servo on pin 9 to the servo object

  Serial.begin(115200);
  randomSeed(analogRead(0));
  Init();
}

void loop() {
  // put your main code here, to run repeatedly:
  int randNumber = random(0, 4);
  Serial.print(randNumber);  Serial.println(" = randnumber ");
  if (gameState == 0) {
    gameState = 1;
    lightUpButton(randNumber);
    //checkforPress(randNumber);
    giveCookie();
  }
}

void lightUpButton(int pinNumber) {
  digitalWrite(ledPins[pinNumber], HIGH);
}

void checkforPress(int pinNumber) {
  int buttonPress = 0;
  while (1) {
    buttonPress = digitalRead(buttonPins[pinNumber]);
    if (buttonPress == HIGH) {
      digitalWrite(ledPins[pinNumber], LOW);
      giveCookie = 1;
      return;
    }
  }

}

void giveCookie() {
  if (giveCookie == 1) {
    int whatServo;
    for (int thisPin = 0; thisPin < numServos; thisPin++) {
      if (hasCookie[thisPin] == 1) {
        hasCookie[thisPin] = 0;
        Serial.print(hasCookie[thisPin]);  Serial.println(" = activeservo =>  ");
        whatServo = thisPin;
        //return;
      }
    }
    Serial.print("open servo : "); Serial.println(whatServo);

    //   if (motor1Up == true) {
    //    myservo1.write(180);              // tell servo to go to position in variable 'pos'
    //    digitalWrite(ledPin, LOW);
    //    delay(15);
    //  } else {
    //    myservo1.write(-120);              // tell servo to go to position in variable 'pos'
    //    delay(15);
    //  }
    delay(2000);
    giveCookie = 0;
    gameState = 0;
  }
}

Last update: May 24, 2021