Help on a quick pill dispenser project

So I'm throwing this thing together real quick. I can't seem to get the sonar to trigger the if and statement. It serial prints that it's detecting movement within range but it still doesn't work. Any ideas?

pill_dispenser.ino (2.39 KB)

Please read the first topic like "How to use this Forum", especially #7, how to post code. Always post the entire code. The snippet You posted does not compile.

Got it fixed!

Good.

Oh sorry just meant I got the post fixed. Could I please get some help on this still?

Sure. Please use the code tags, left symbol up in this window for posting code. That makes more helpers able to read Your code and give replies.

Hey everybody. Just looking for some help. It looks like this code isn't running. It's not tripping the if statement even though the conditions are met. Thanks!

#include <Stepper.h>
#include <NewPing.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 30 // Maximum distance we want to ping for (in centimeters).

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
const int stepsPerRevolution = 64 * 32; // set number of steps
Stepper myStepper(stepsPerRevolution, 2, 4, 3, 5); // set stepper to pins 2-5
LiquidCrystal_I2C lcd(0x27, 16, 2); //set lcd display to address

// set up variables
int pill = 7; // set pill count
int uScm = 0; // set distance to 0 for now
int inPin = 7;
int reset = 0;
// Pins 2-5 stepper
// Pin 7 button
// Pins 11&12 sonar
// Pins A4-A5 Display


void setup() {
  int(inPin, INPUT);
  lcd.init(); //Set up display
  myStepper.setSpeed(15); // set stepper speed to 15 rpm;
  Serial.begin(9600); // initialize the serial port:
}

void loop() {
  reset = digitalRead(inPin);
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  uScm = uS / US_ROUNDTRIP_CM; // convert to cm
  Serial.println(uScm);
  Serial.println(pill);
  Serial.print(pill);

  if ((pill > 0) && (uScm > 1)) { //if there's a pill left and sonar is reading motion
    myStepper.step(stepsPerRevolution / 7); // rotate the stepper 360/7 to dispense one pill:
    pill = pill - 1; //decrease pill counter by 1
    lcd.setBacklight(HIGH); //turn on display
    //diplay pill
    lcd.setCursor(0, 0);
    lcd.print(pill);
    lcd.setCursor (3, 0);
    lcd.print("Days Left");
    delay (3000); //delay 3 sec
    lcd.setBacklight(LOW); //turn off display
  }

  if (pill = 0) {
    //diplay refill
    lcd.setBacklight(HIGH);
    lcd.setCursor(0, 0);
    lcd.print("Please Refill");
    delay (3000); //delay 3 sec
    lcd.setBacklight(LOW); //turn off display
  }

  if (reset == LOW) { //If button push reset pill counter to 7 display count
    pill = 7;
    //diplay pill
    lcd.setBacklight(HIGH);
    lcd.setCursor(0, 1);
    lcd.print(pill);
    lcd.setCursor (3, 1);
    lcd.print("Days Left");
    lcd.setCursor (0, 0);
    lcd.print("Refilled");
    delay (3000); //delay 3 sec
    lcd.setBacklight(LOW); //turn off display
  }
  else {
    delay (50);
  }
}
  if (pill = 0) {

You sure?

How is the button wired? Looks like you are looking for a LOW when pressed, so, button should be wired between GND and pin 7 with a pullup resistor from pin 7 to Vcc or internal pullup enabled. Is that correct? Are you getting ANY prints?

A schematic not a frizzy thing showing all connections would be a big help. Also links to all the hardware devices. Remember it does what you tell it to. I have a hard time believing all the conditions are met for the if statement. You may think so but it is disagreeing with you. It is possible the hardware is bad.

Paul__B:

  if (pill = 0) {

You sure?

Good catch- I wonder how long until other posters see the same problem.

Don't double-post.

Duplicate topics merged

Why did you start a second one ?

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.