ระบบสายพานลำเลียง

วีดีโอการประกอบระบบสายพานลำเลียงอัตโนมัติ

โคดสำหรับ Arduino IDE

Arduino IDE Code

#include <Servo.h>

#include <Wire.h>

#include <LCD.h>

#include <LiquidCrystal_I2C.h>

#define ref0 500 // sensor A0 GP

#define ref1 500

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int outputEnabled = 2;

const int s0 = 3;

const int s1 = 4;

const int s2 = 5;

const int s3 = 6;

const int nLED = 7;

const int out = 8;

int red = 0;

int green = 0;

int blue = 0;

int x;

/////// Motor DC /////////

int dir1PinA = 10;

int dir2PinA = 11;

int speedPinA = 9;

Servo myservo;

//////////////////////////////////////////////////////////

void setup()

{

servo_set();

motor_stop();

pinMode(outputEnabled, OUTPUT);

pinMode(s0, OUTPUT);

pinMode(s1, OUTPUT);

pinMode(s2, OUTPUT);

pinMode(s3, OUTPUT);

pinMode(nLED, OUTPUT);

pinMode(out, INPUT);

Serial.begin(9600);

digitalWrite(outputEnabled, LOW);

digitalWrite(s0, HIGH);

digitalWrite(s1, HIGH);

digitalWrite(nLED, LOW);

lcd.begin();

lcd.backlight();

lcd.setCursor(0, 0);

lcd.print(" STEM OBEC ");

lcd.setCursor(0, 1);

lcd.print(" Robotics");

lcd.setCursor(12, 1);

delay(2500);lcd.clear();

lcd.print("CONVEYOR PROJECT ");

lcd.setCursor(0, 1);

lcd.print(" Scan Press");

lcd.setCursor(12, 1);

buton();

//analcd();

}

//////////////////////////////////////////////////////////

void lcd_working() {lcd.clear();lcd.setCursor(0, 0);lcd.print("System Working.."); }

void lcd_stop() {lcd.clear();lcd.setCursor(0, 0);lcd.print(" Motor Stop"); }

void servo_set() {myservo.attach(12);myservo.write(10); }

void servo_up() {myservo.attach(12);myservo.write(90); }

void forward() {analogWrite(speedPinA, 255);digitalWrite(dir1PinA,LOW ); digitalWrite(dir2PinA,HIGH );}

void backward() {analogWrite(speedPinA, 255);digitalWrite(dir2PinA, HIGH); digitalWrite(dir1PinA,LOW );}

void motor_stop(){analogWrite(speedPinA, 0);digitalWrite(dir2PinA, LOW); digitalWrite(dir1PinA, HIGH);}

//////////////////////////////////////////////////////////

void ana() // GP Scan

{while(true){ {int s0 = analogRead(A0);Serial.println(s0);delay(10);}}}

//////////////////////////////////////////////////////////

void analcd() // GP Scan

{while(true){ {lcd.clear();Serial.println(analogRead(A0));Serial.println(analogRead(A1));lcd.setCursor(1, 1);lcd.print(analogRead(A0));lcd.setCursor(10, 1);lcd.print(analogRead(A1));delay(500);}}}

/////////////////////////////////////////////////////////

void gp() // GP Scan

{lcd_working();while(true){ analogRead(A0);analogRead(A1);forward();

if(analogRead(A0)>ref0) {gp1();}

else if (analogRead(A1)>ref1){setup(); }}}

/////////////////////////////////////////////////////////

void gp1() // GP Scan

{while(true){ analogRead(A0);analogRead(A1);forward();

if(analogRead(A0)<ref0-100) {lcd_stop() ;motor_stop(); delay(2000);gy31();}

else if (analogRead(A1)>ref1){setup(); }}}

/////////////////////////////////////////////////////////

void buton() // GP Scan

{while(true){ analogRead(A1);if(analogRead(A1)>ref1) {lcd_working();delay(1500);gp();}

}}

///////////////////////////////////////////////////////////

/////////////////SCAN GRB GY-31//////////////////////////////

void gy31() // ใช้สแกนสี RGB

{while(true){

color();

lcd.clear();

lcd.setCursor(4, 0);lcd.print("Process");

Serial.print("R");

Serial.print(red, DEC);

Serial.print(" G");

Serial.print(green, DEC);

Serial.print(" B");

Serial.print(blue, DEC);

Serial.println();

analogRead(A1);

//Simple logic to test for color

if (red < blue && red < green){Serial.println("Red");lcd.setCursor(6, 1);lcd.print("Red");servo_up();delay(2000);servo_set();delay(3000);gp();}

else if (blue < red && blue < green) {Serial.println("blue");lcd.setCursor(6, 1);lcd.print("blue");servo_set();delay(3000);gp();}

else if (analogRead(A1)>ref1){setup();}

else {Serial.println("green");lcd.setCursor(6, 1);lcd.print("green");servo_set();delay(3000);gp();}

delay(1000);}}

///////////////////////////////////////////////////////////

void color() {

digitalWrite(nLED,1);delay(100);

digitalWrite(s2, LOW);

digitalWrite(s3, LOW);

// count OUT, pRed, RED

red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);

digitalWrite(s3, HIGH);

//count OUT, pBLUE, BLUE

blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);

digitalWrite(s2, HIGH);

// count OUT, pGreen, GREEN

green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);

digitalWrite(nLED,0);

}

/////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////

void loop() { }

คู่มือการใช้งาน : https://drive.google.com/file/d/0BweHqKEWNTOFTS0ybDhQM3BzNlE/view?usp=sharing