Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- ekasinambela
- Date:
- 2016-06-19
- Revision:
- 1:8431b042a2eb
- Parent:
- 0:29fc24ce612f
- Child:
- 2:5b26538ab9ab
File content as of revision 1:8431b042a2eb:
#include "mbed.h" #include "DRV8825.h" #define MAX_SPEED 8000 #define MICROSTEPS_PER_STEP 32 #include "Ping.h" #include <stdio.h> #include "TextLCD.h" #include <cmath> // add boundaries Ping Pinger(p30); Serial pc(USBTX, USBRX); //TextLCD lcd(p19, p20, p21, p22, p23, p24); // rs, e, d4-d7 //DigitalOut myled(LED1); int PULL = 1; int PUSH = 0; int SALT = 1; int PURE = 0; int PREVIOUS_DIRECTION_SALT = -1; int PREVIOUS_DIRECTION_PURE = -1; //int POSITION_SALT = 14; //int POSITION_PURE = 27; //int MAX = 35; //int MIN = 0; DRV8825 stpr_mtr(p21, p27, p28, p29, p22, p23); DRV8825 stpr_mtr2(p24, p27, p28, p29, p25, p26); bool proximity_ok(){ int range; Pinger.Send(); wait(0.1); range = Pinger.Read_cm(); range = range/2; // lcd.printf("Range in cm: %d \n\r", range); if (range >= 12){ //lcd.printf("Proximity ok"); return true; } return false; } void add_water(int type, double mililiters, int DIRECTION){ int MOVEMENT = 4600; //constant for 1 mL double STEP_EXTRA = 3000; //for the other mL's //accelerate for (int i = 25; i < MAX_SPEED; i+=20) { if (type == SALT) stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i); else stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i); } //fix the changing of directions for pure if (type == PURE){ //mililiters = mililiters * 2.0; if (PREVIOUS_DIRECTION_PURE != DIRECTION) mililiters = mililiters + 1.0; } //fix the changing of directions for salt if (type == SALT){ //mililiters = mililiters * 2.0; if (PREVIOUS_DIRECTION_SALT != DIRECTION) mililiters = mililiters + 1.0; //if (PREVIOUS_DIRECTION_SALT == PULL) //mililiters = mililiters + 0.5; } //move with constant speed for (int i = 1; i < (MOVEMENT + (int)floor(((mililiters - 1.0)*STEP_EXTRA))); i+=1) { if (type == SALT) stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, MAX_SPEED); else stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, MAX_SPEED); //if ((i - MOVEMENT) % STEP_EXTRA == 0 && !proximity_ok()) // break; } //de-accelerate for (int i = MAX_SPEED; i > 0; i-=20) { if (type == SALT) stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i); else stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i); } //remember previous direction if (type == SALT){ PREVIOUS_DIRECTION_SALT = DIRECTION; }else{ PREVIOUS_DIRECTION_PURE = DIRECTION; } } int main() { //add_water(PURE, -1, PUSH); //add_water(SALT, -1, PUSH); //if(proximity_ok()){ //add_water(SALT, 1, PULL); //lcd.printf(" GO"); //add_water(SALT, 2, PUSH); add_water(PURE, 2.0, PUSH); wait(2); add_water(PURE, 2.0, PULL); wait(2); add_water(PURE, 2.0, PUSH); wait(2); add_water(PURE, 2.0, PULL); wait(2); add_water(PURE, 2.0, PUSH); wait(2); add_water(PURE, 2.0, PULL); //add_water(SALT, 2, PULL); /* wait(2); add_water(SALT, 1, PULL); wait(2); add_water(SALT, 2, PUSH); wait(2); add_water(SALT, 3, PULL); //add_water(PURE, 1, PUSH); //wait(2); */ //} /* //add_water(SALT, 2, PULL); if(proximity_ok()){ add_water(SALT, 2, PUSH); wait(2); } //add_water(SALT, 3, PULL); if(proximity_ok()){ add_water(SALT, 3, PUSH); wait(2); } add_water(SALT, 2, PUSH); //add_water(PURE, 1, PULL); //add_water(PURE, 1, PUSH); //add_water(PURE, 2, PULL); //add_water(PURE, 2, PUSH); //add_water(PURE, 3, PULL); //add_water(PURE, 3, PUSH); */ }