Skip to content

Initial Coding

Components

This task was about compiling all the available components together and combining their code. In this code I compiled these components together in the Arduino mega:

  • LCD screen

  • Joy stick

  • 3 Stepper motors

  • Accelerometer

  • Speaker module

So what the purpose of each of these components?

The joy stick will controls the LCD screen to show information of each medicine type we have, the stepper motors, each will control the dispensing of each pill type, and the Accelerometer will control the speaker module.

So how the device works and what each component add to the device?

Basically, the device will have a pre-set schedule of the timing of each pill, when the of any of the pill arrives, the correspondent stepper motor will rotate dispensing the pill. The pill will be held in some sort of door that hooked to the accelerometer, once teh doors opens, the accelerometer will get inclined and it will activates the speaker. The speaker will store encouraging messages from the user’s loved ones to associates the pill time with positivity.


Coding

Now we understands the purpose of each component, let’s move to coding.

Here is the code that I wrote compiling all the mentioned above components with detailed comments explaining the steps that I went through:

Click to expand
#include <Stepper.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MPU6050.h> //accelerometer
#include <Adafruit_Sensor.h>  //accelerometer

Adafruit_MPU6050 mpu; // For accelerometer

#define PLAY_E 2 // defining pin 2 to playPack_edge
LiquidCrystal_I2C lcd(0x27,  16, 2);

const int stepsPerRevolution = 1100; // change this to fit the number of steps per revolution
int rpm = 17;

// initialize stepper1 library on pins 8 through 11:
Stepper Stepper1(stepsPerRevolution, 8, 10, 9, 11);

// Initialize the stepper library on pins 3 through 6
Stepper Stepper2(stepsPerRevolution, 4, 3, 5, 6);

// Initialize the stepper library on pins 45 through 47
Stepper Stepper3(stepsPerRevolution, 47, 45, 44, 46);

// define the pins of the joy stick.
int xPin = A0;
int yPin = A1; 

// define veriables to the values, to read later. 
int xPosition = 0;
int yPosition = 0;

// map function sets the limits for the joy stick
int mapX = 0;
int mapY = 0;

int motor = 1;
void setup() {
  // Set the speed of the motors (in RPM)
  Stepper1.setSpeed(rpm);
  Stepper2.setSpeed(rpm);
  Stepper3.setSpeed(rpm);

  lcd.init(); // initilize the screen
  lcd.backlight(); // Turn on the light of the screen
  lcd.clear(); // Clears any previous data in the screen
  Serial.begin(9600); 

  // define the state of the pins
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);

 // dedine the speaker pin
 pinMode(PLAY_E, OUTPUT);

  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens
  // Try to initialize!
  if (!mpu.begin()) {
    while (1) {
      delay(10);
    }
  }
}

void loop() {

  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);

// target = map(source, sourceLow, sourceHigh, targetLow, targetHigh) 

  mapX = map(xPosition, 0, 1023, -512, 512);
  mapY = map(yPosition, 0, 1023, -512, 512);

if( ( mapX <-200) && (mapY > 200)) {
lcd.setCursor(0,0);
lcd.print("Medication Type: ");

lcd.setCursor(0,1);
lcd.print("Panadol   ");


 Stepper1.step(stepsPerRevolution);
}

if((mapX >200) && (mapY <200)) {
lcd.setCursor(0,0);
lcd.print("Medication Type: ");

lcd.setCursor(0,1);
lcd.print("Accutane ");

 Stepper1.step(stepsPerRevolution);
}

if((mapX <200) && (mapY <-200)){
lcd.setCursor(0,0);
lcd.print("Medication Type: ");

lcd.setCursor(0,1);
lcd.print("strepsils ");

Stepper2.step(stepsPerRevolution);
}

if((mapX >-200) && (mapY >200)) {
lcd.setCursor(0,0);
lcd.print("Medication Type: ");

lcd.setCursor(0,1);
lcd.print("Vicks    ");

Stepper3.step(stepsPerRevolution);
}

 /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

if (a.acceleration.y < -5) {
digitalWrite(PLAY_E, HIGH);
delay(500);
digitalWrite(PLAY_E, LOW);


}

}

Not that this is not the final code, the components here are missing the Wi-Fi module which is essential to the scheduling of the pills and connecting app. Additionally, since the Wi-Fi is not ready yet and my group mate Al jeshi was working on it individually, I put the motor command below the joy stick movement. Therefore, with each direction of the joy stick (Up, Down, Left, Right) the motor will start as well, the screen will change. meaning that the joy stick is controlling the screen and the motors.


Connection Diagram:

Here is the connection diagram:


Last update: September 14, 2024