자판기 코드입니다.
lcd 화면엔 계속해서 coin 수와 상품 번호가 뜨고 있으며
원리는 동전을 넣었을 때 초음파 센서가 인식하여 coin수가 증가하고
상품 번호를 선택하면 서보모터가 작동하는 것입니다.
#include <LiquidCrystal.h>
#include <Servo.h>
Servo myservo;
Servo yourservo;
int myangle=90;
int yourangle=90;
long bounceStartTime=0;
long debounce=50;
LiquidCrystal lcd(5,6,7,11,12,13);
int trig=0; //초음파센서의 송신부
int echo=1; //초음파센서의 수신부
int sw1 = 8 ;
int sw2 = 9 ;
int sw3 = 10 ;
//int sw4 = 11 ;
long object ;
int coin;
int ledR=A3;
int ledG=A2;
void setup()
{
Serial.begin(9600);
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
lcd.begin(16,2);
lcd.clear();
myservo.attach(A0);
yourservo.attach(A1);
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(sw3, INPUT_PULLUP);
// pinMode(sw4, INPUT_PULLUP);
pinMode(ledR,OUTPUT);
pinMode(ledG,OUTPUT);
}
void loop()
{
digitalWrite(ledR,HIGH);
digitalWrite(ledR,LOW);
delay(500);
digitalWrite(ledG,HIGH);
digitalWrite(ledG,LOW);
coin = 0 ;
object=0;
lcd.setCursor(0,0);
lcd.print(" coin : ");
lcd.print(coin);
lcd.setCursor(0,1);
lcd.print(" number : ");
lcd.print(object);
digitalWrite(trig,LOW);
digitalWrite(echo,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
unsigned long duration=pulseIn(echo,LOW);
float distance=duration/29.0/2.0;
Serial.print(distance);
Serial.println("cm");
while(1)
{
Serial.print(distance);
Serial.println("cm");
if(distance<5.0)
{
coin++;
}
lcd.setCursor(0,0);
lcd.print("coin : ");
lcd.print(coin);
if (digitalRead(sw3) == LOW)
break;
}
if (digitalRead(sw1) == LOW)
{
object=1;
}
if (digitalRead(sw2) == LOW)
{
object=2;
}
/*if (digitalRead(sw3) == LOW)
{
digitalWrite(object = 3);
}*/
lcd.setCursor(0,0);
lcd.print("coin : ");
lcd.print(coin);
lcd.setCursor(0,1);
lcd.print("number : ");
lcd.print(object); //입력 한 물건 숫자 출력
if(object == 1)
{myangle +=89 ;}
else if(object == 2)
{myangle -= 89;}
else
{yourangle += 89;}
myservo.write(myangle);
yourservo.write(yourangle);
coin = 0;
bounceStartTime == 0;
delay(3000);
myservo.write(0);
yourservo.write(0);
}
|