Skip to content

Instantly share code, notes, and snippets.

@arn-ob
Last active April 13, 2018 10:04
Show Gist options
  • Select an option

  • Save arn-ob/f9a16098af54f05bf563df020e384867 to your computer and use it in GitHub Desktop.

Select an option

Save arn-ob/f9a16098af54f05bf563df020e384867 to your computer and use it in GitHub Desktop.
Bottle Cutting Mechine Arduino Source Code [ProjectCity Project Source Code]
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <TinyGPS++.h> // Tiny GPS Plus Library
#include <SoftwareSerial.h> // Software Serial Library so we can use other Pins for communication with the GPS module
const char* ssid = "project123";
const char* password = "project123";
// Create an instance of the server
// specify the port to listen on as an argument
// Gps setings
static const int RXPin = 5, TXPin = 4; // Ublox 6m GPS module to pins 12 and 13
// 5 = D1 => Tx
// 4 = D2 => Rx
static const uint32_t GPSBaud = 9600; // Ublox GPS default Baud Rate is 9600
const double Home_LAT = 23.815240; // Your Home Latitude
const double Home_LNG = 90.425849; // Your Home Longitude
TinyGPSPlus gps; // Create an Instance of the TinyGPS++ object called gps
SoftwareSerial ss(RXPin, TXPin); // The serial connection to the GPS device
double late;
double lang;
// End Define
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("GPS example");
Serial.println(TinyGPSPlus::libraryVersion());
delay(1500); // Pause 1.5 seconds
ss.begin(GPSBaud); // Set Software Serial Comm Speed to 9600
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
pinMode(12, INPUT);
pinMode(13, INPUT);
}
void loop() {
Serial.println("Loop");
String Lmsg = "http://server.projectcitybd.com/coinCount.php?c=LargeCoin&lat=" + String(late) + "&lng=" + String(lang);
String Smsg = "http://server.projectcitybd.com/coinCount.php?c=SmallCoin&lat=" + String(late) + "&lng=" + String(lang);
if (digitalRead(12) == HIGH) {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
delay(100);
sendData(Lmsg);
Serial.println(Lmsg);
Serial.println("Large Coin");
}
if (digitalRead(13) == HIGH) {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
delay(100);
sendData(Smsg);
Serial.println(Smsg);
Serial.println("Small Coin");
}
WiFi.disconnect();
delay(500);
}
void sendData(String msg) {
HTTPClient http;
http.begin(msg);
int httpCode = http.GET();
if (httpCode > 0) {
//Check the returning code
Serial.println(String(httpCode));
//Close connection
http.end();
Serial.println("Data Send");
} else {
// error code
Serial.println(String(httpCode));
//Close connection
http.end();
Serial.println("Can't Send to the server");
// Busy
Serial.println("Server Busy");
// Delay
delay(500);
}
gps_date();
delay(1000);
}
void gps_date() {
late = gps.location.lat();
lang = gps.location.lng();
smartDelay(500); // Run Procedure smartDelay
if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
}
static void smartDelay(unsigned long ms) // This custom version of delay() ensures that the gps object is being "fed".
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
#include <TinyGPS++.h> // Tiny GPS Plus Library
#include <SoftwareSerial.h> // Software Serial Library so we can use other Pins for communication with the GPS module
#include <Adafruit_ssd1306syp.h> // Adafruit oled library for display
Adafruit_ssd1306syp display(4,5); // OLED display (SDA to Pin 4), (SCL to Pin 5)
static const int RXPin = 12, TXPin = 13; // Ublox 6m GPS module to pins 12 and 13
// 12 = D6 => Tx
// 13 = D7 => Rx
static const uint32_t GPSBaud = 9600; // Ublox GPS default Baud Rate is 9600
const double Home_LAT = **.******; // Your Home Latitude
const double Home_LNG = **.******; // Your Home Longitude
TinyGPSPlus gps; // Create an Instance of the TinyGPS++ object called gps
SoftwareSerial ss(RXPin, TXPin); // The serial connection to the GPS device
void setup()
{
display.initialize(); // Initialize OLED display
display.clear(); // Clear OLED display
display.setTextSize(1); // Set OLED text size to small
display.setTextColor(WHITE); // Set OLED color to White
display.setCursor(0,0); // Set cursor to 0,0
display.println("GPS example");
display.println(TinyGPSPlus::libraryVersion());
display.update(); // Update display
delay(1500); // Pause 1.5 seconds
ss.begin(GPSBaud); // Set Software Serial Comm Speed to 9600
}
void loop()
{
display.clear();
display.setCursor(0,0);
display.print("Latitude : ");
display.println(gps.location.lat(), 5);
display.print("Longitude : ");
display.println(gps.location.lng(), 4);
display.print("Satellites: ");
display.println(gps.satellites.value());
display.print("Elevation : ");
display.print(gps.altitude.feet());
display.println("ft");
display.print("Time UTC : ");
display.print(gps.time.hour()); // GPS time UTC
display.print(":");
display.print(gps.time.minute()); // Minutes
display.print(":");
display.println(gps.time.second()); // Seconds
display.print("Heading : ");
display.println(gps.course.deg());
display.print("Speed : ");
display.println(gps.speed.mph());
unsigned long Distance_To_Home = (unsigned long)TinyGPSPlus::distanceBetween(gps.location.lat(),gps.location.lng(),Home_LAT, Home_LNG);
display.print("KM to Home: "); // Have TinyGPS Calculate distance to home and display it
display.print(Distance_To_Home);
display.update(); // Update display
delay(200);
smartDelay(500); // Run Procedure smartDelay
if (millis() > 5000 && gps.charsProcessed() < 10)
display.println(F("No GPS data received: check wiring"));
}
static void smartDelay(unsigned long ms) // This custom version of delay() ensures that the gps object is being "fed".
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
// include the library code:
#include <LiquidCrystal.h>
#include <HX711.h>
#include <Servo.h>
// Pin define
#define CLK 30
#define DOUT 31
#define BIG_SERVO 52
#define METAL_SENSOR 51
#define MOTOR_RELAY 53
#define SOUND 50
#define COIN_SERVO_A 48
#define COIN_SERVO_B 49
#define large_coin 43
#define small_coin 42
#define MOTOR_Delay 2000
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// For weight
HX711 scale(DOUT, CLK);
float calibration_factor = 22300.00;
float units;
float ounces;
// Servo
Servo bigServo;
Servo coinServoA;
Servo coinServoB;
void setup()
{
Serial.begin(9600);
// Pin setup
pinMode(BIG_SERVO, OUTPUT);
pinMode(METAL_SENSOR, INPUT);
pinMode(MOTOR_RELAY, OUTPUT);
pinMode(SOUND, OUTPUT);
pinMode(large_coin, OUTPUT);
pinMode(small_coin, OUTPUT);
// LCD :
lcd.begin(16, 2);
lcd.print("Initialization");
// Weight :
scale.set_scale();
scale.tare();
long zero_factor = scale.read_average(); //Get a baseline reading
// Servo
// Servo 1
bigServo.attach(BIG_SERVO);
coinServoA.attach(COIN_SERVO_A);
coinServoB.attach(COIN_SERVO_B);
delay(10);
bigServo.write(5);
delay(100);
coinServoA.write(0);
delay(100);
coinServoB.write(90);
delay(100);
}
void loop()
{
// lcd.clear();
// lcd.print("Weight");
// lcd.setCursor(0, 1);
// lcd.print(weight());
digitalWrite(small_coin, LOW);
digitalWrite(large_coin, LOW);
// Metal Detect
if (digitalRead(METAL_SENSOR) == HIGH)
{
// Accurate the Weight
delay_weight();
// Weight Range 1
if (weight() >= 100.20 && weight() <= 100.39) // For Small Bottle
{
Serial.println("Small Bottle");
lcd.clear();
lcd.print("Detected");
lcd.setCursor(0, 1);
lcd.print("Small Bottle");
digitalWrite(MOTOR_RELAY, HIGH); // Motor Run
delay(30);
bigServo.write(180); // Big Servo Run 180 angle
delay(30);
coinServoA.write(90); // Coin Servo A 90 angle
delay(100);
digitalWrite(small_coin, HIGH);
delay(200);
digitalWrite(small_coin, LOW);
delay(1000);
coinServoA.write(0); // Coin Servo A 0 angle
delay(1000);
bigServo.write(5); // Big Servo Run 5 angle
delay(30);
delay(MOTOR_Delay);
digitalWrite(MOTOR_RELAY, LOW); // Motor End
delay(500);
}
// Weight Range 2
else if (weight() >= 100.30 && weight() <= 100.70) // For Large Bottle
{
Serial.println("Large Bottle");
lcd.clear();
lcd.print("Detected");
lcd.setCursor(0, 1);
lcd.print("Large Bottle");
delay(30);
digitalWrite(MOTOR_RELAY, HIGH); // Motor Run
delay(30);
bigServo.write(180); // Big Servo Run 180
delay(100);
digitalWrite(large_coin, HIGH);
delay(200);
digitalWrite(large_coin, LOW);
delay(30);
coinServoB.write(0); // Coin Servo B 0 angle
delay(1000);
coinServoB.write(90); // Coin Servo B 90 angle
delay(1000);
bigServo.write(5); // Big Servo Run 5 Angle
delay(30);
delay(MOTOR_Delay);
digitalWrite(MOTOR_RELAY, LOW); // Motor END
delay(500);
}
// Not in the range
else if (weight() >= 100.90)
{ // This for if the weight not in the weight range
lcd.clear();
lcd.print("Warning");
lcd.setCursor(0, 1);
lcd.print("Over Weight");
digitalWrite(SOUND, HIGH);
Serial.println("Over Weight");
bigServo.write(5);
delay(200);
digitalWrite(SOUND, LOW);
}
else
{
Serial.println("Not in range");
}
}
// Metal Detect
if (digitalRead(METAL_SENSOR) == LOW)
{
digitalWrite(SOUND, HIGH);
lcd.clear();
lcd.print("Detected");
lcd.setCursor(0, 1);
lcd.print("Metal");
delay(200);
lcd.clear();
digitalWrite(SOUND, LOW);
Serial.println("Metal");
}
// Default State
lcd.clear();
lcd.print("Ready");
lcd.setCursor(0, 1);
lcd.print("Empty");
Serial.println("Default Code Running");
Serial.print(weight());
Serial.print(" grams");
Serial.println();
}
// Get Delay for getting accurate weight
void delay_weight(){
Serial.println("No metal");
delay(50);
Serial.println(weight());
delay(50);
Serial.println(weight());
delay(50);
Serial.println(weight());
delay(50);
}
float weight()
{
scale.set_scale(calibration_factor); //Adjust to this calibration factor
units = scale.get_units(), 10;
if (units < 0)
{
units = 0.00;
}
units = units;
ounces = units * 0.335274;
delay(100);
return ounces + 100;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment