By: Mohamed Ihsan

Project Partner: Changqi Yu

Written On: 10/31/2021

Project 2 Part 1: Working on the Circuitry

Changqi and I decided to meet twice this week to work on this project. For our first meeting, we decided to just do the hardware side of the project, without yet programming the P5 side. However, we began to think about the P5 interface.

First, we started by creating the schematic for our project.

Untitled

From making this schematic, we decided that we would use a rotary encoder (and perhaps eventually get around to making 3D-printed gears to use a handle to turn this rotary encoder).

Untitled

We therefore also went through the Sensor Lab on rotary encoders, to understand how to wire and use rotary encoders. The Code for using a rotary encoder is as follows:

#include <Encoder.h>
 
// encoder on pins 2 and 3
Encoder myEncoder(2, 3);
// previous position of the encoder:
int lastPosition = 0;
 
// steps of the encoder's shaft:
int steps = 0;
 
 
const int buttonPin = 4;    // pushbutton pin
int lastButtonState = LOW;  // last button state
int debounceDelay = 5;       // debounce time for the button in ms
 
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
}
 
void loop() {
  // read the pushbutton:
  int buttonState = digitalRead(buttonPin);
  //  // if the button has changed:
  if (buttonState != lastButtonState) {
    // debounce the button:
    delay(debounceDelay);
    // if button is pressed:
    if (buttonState == LOW) {
      Serial.print("you pressed on ");
      Serial.println(steps);
    }
  }
  // save current button state for next time through the loop:
  lastButtonState = buttonState;
 
  // read the encoder:
  int newPosition = myEncoder.read();
  // compare current and last encoder state:
  int change = newPosition - lastPosition;
  // if it's changed by 4 or more (one detent step):
  if (abs(change) >= 32) {
    // get the direction (-1 or 1):
    int encoderDirection = (change / abs(change));
    steps += encoderDirection;
    // if you want to make the steps rollover, use this:
    if (steps < 0) steps = 23;
    steps = steps % 24;
    Serial.println(steps);
 
    // save encoder position for next time through loop:
    lastPosition = newPosition;
  }
}

Untitled

From here, we went ahead and decided to wire together our project on our breadboards:

Untitled

As shown above, we didn't wire all 10 buttons we had in mind for our project. For this week, we decided to first get a simpler version of the project working. After wiring the above, we also decided to test some buttons on Arduino with this hardware (to see that it at least works). The code for that test is as follows (it's basically a merge of the encoder lab, but using a separate button rather than the one built into the rotary encoder):

#include <Encoder.h>
 
// encoder on pins 2 and 3
Encoder myEncoder(2, 3);
// previous position of the encoder:
int lastPosition = 0;
 
// steps of the encoder's shaft:
int steps = 0;

const int buttonStart = 4;
const int bananaButton = 5;
const int appleButton = 6;
const int durianButton = 7;
 
 
const int buttonPin = 4;    // pushbutton pin
int lastButtonState = LOW;  // last button state
int debounceDelay = 5;       // debounce time for the button in ms
 
void setup() {
  Serial.begin(9600);
  //pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonStart, INPUT);
  pinMode(bananaButton, INPUT);
  pinMode(appleButton, INPUT);
  pinMode(durianButton, INPUT);
}
 
void loop() {
  // read the pushbutton:
  int buttonState = digitalRead(buttonStart);
  //  // if the button has changed:
  if (buttonState != lastButtonState) {
    // debounce the button:
    delay(debounceDelay);
    // if button is pressed:
    if (buttonState == LOW) {
      Serial.print("you pressed on ");
      Serial.println(steps);
    }
  }
  // save current button state for next time through the loop:
  lastButtonState = buttonState;
 
  // read the encoder:
  int newPosition = myEncoder.read();
  // compare current and last encoder state:
  int change = newPosition - lastPosition;
  // if it's changed by 4 or more (one detent step):
  if (abs(change) >= 32) {
    // get the direction (-1 or 1):
    int encoderDirection = (change / abs(change));
    steps += encoderDirection;
    // if you want to make the steps rollover, use this:
    if (steps < 0) steps = 23;
    steps = steps % 24;
    Serial.println(steps);
 
    // save encoder position for next time through loop:
    lastPosition = newPosition;
  }
}

Test of our hardware for our project

Test of our hardware for our project

Untitled

As shown in this image, this is sort of the setup we have in mind for now for our project. There is a smoothie machine and drink below it (both shown on the right). On the bottom left would be a progress counter, and the top left is the "recipe" you have to make.