번개애비의 라이프스톼일

고양이 레이저 장난감 만들기 (아두이노) 본문

IT

고양이 레이저 장난감 만들기 (아두이노)

번개애비 2019. 8. 5. 02:35

서보모터 2개와 레이저모듈 1개, 아두이노를 이용하여 레이저 장난감을 만들어보았습니다.

on/off 스위치를 통해 아날로그의 신호를 받아 on인지 off인지를 체크하여 장난감이 작동을 시작하는지 판단하여 작동되며, 레이저가 on/off를 주기적으로 실행하여 고양이들에게 관심을 줄 수 있도록 제작하였습니다.

 

 

 

#include <Servo.h>
int cds = A5; 
int laser = 2;
int pos = 0;
Servo myservo11;
Servo myservol2;

void setup() {                     
  Serial.begin(9600); 
  pinMode(laser, OUTPUT);
  myservo11.attach(9);
  myservol2.attach(10);
}
void loop() {                                
  int val= analogRead(cds); 
  delay(1000); 
  Serial.println(val);
  if(val>=1023){
      Serial.println("START!!!!!");
      Serial.println("SPLASH");
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      for (pos = 50; pos <= 230; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(35); 
      }
      Serial.println("SPLASH");
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      for (pos = 230; pos >= 50; pos -= 1) { 
        myservo11.write(pos);
        myservol2.write(pos);
        delay(35); 
      }
      Serial.println("SPLASH");
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
       for (pos = 50; pos <= 100; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(15); 
      }
      for (pos = 50; pos <= 100; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(15); 
      }
       for (pos = 50; pos <= 180; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(7); 
      }
      for (pos = 50; pos <= 180; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(7); 
      }
      Serial.println("SPLASH");
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      for (pos = 50; pos <= 230; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(5); 
      }
      digitalWrite(laser,HIGH);
      for (pos = 50; pos <= 230; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(5); 
      }
     Serial.println("SPLASH");
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
       for (pos = 50; pos <= 230; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(2); 
      }
      for (pos = 50; pos <= 230; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(2); 
      }
      for (pos = 230; pos >= 50; pos -= 1) { 
        myservo11.write(pos);
        myservol2.write(pos);
        delay(35); 
      }
      for (pos = 50; pos <= 230; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(35); 
      }
      for (pos = 230; pos >= 50; pos -= 1) { 
        myservo11.write(pos);
        myservol2.write(pos);
        delay(2); 
      }
      for (pos = 50; pos <= 230; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(2); 
      }
      for (pos = 230; pos >= 50; pos -= 1) { 
        myservo11.write(pos);
        myservol2.write(pos);
        delay(15); 
      }
      for (pos = 50; pos <= 230; pos += 1) {
        myservo11.write(pos);  
        myservol2.write(pos);
        delay(15); 
      }
      Serial.println("SPLASH");
      digitalWrite(laser,LOW);
      delay(200); 
      digitalWrite(laser,HIGH);
      delay(200); 
      digitalWrite(laser,LOW);
      delay(200); 
      digitalWrite(laser,HIGH);
      delay(200); 
      digitalWrite(laser,LOW);
      delay(200); 
      digitalWrite(laser,HIGH);
      delay(200); 
      digitalWrite(laser,LOW);
      delay(200); 
      digitalWrite(laser,HIGH);
      delay(200); 
      digitalWrite(laser,LOW);
      delay(200); 
      digitalWrite(laser,HIGH);
      delay(200); 
      digitalWrite(laser,LOW);
      delay(200); 
      digitalWrite(laser,HIGH);
      delay(200); 
      digitalWrite(laser,LOW);
      delay(200); 
      digitalWrite(laser,HIGH);
      delay(200); 
      digitalWrite(laser,LOW);
      delay(200); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      delay(100); 
      digitalWrite(laser,LOW);
      delay(100); 
      digitalWrite(laser,HIGH);
      Serial.println("FINISH!!!");
    }else{
       digitalWrite(laser,LOW);
    }
 }

참으로 무식한 코딩이 아닐수가 없네요 ㅎㅎㅎ

 

 

 

아두이노는 참 오랫만에 만져보네요.

온오프 스위치는 5v 전원과 아날로그 5번핀에 물려있습니다.

Off 상태일때는 1000미만의 값이 나오고 On 상태일때 (5v가 인가된 상태일때)는 1023 이상의 값이 출력되는 것을 이용하여 On 동작을 수행할 수 있도록 고안하였습니다.

 

 

서보모터의 고정은 글루건과 케이블타이로도 충분히 가능했습니다.

흔히 우리가 생각하는 3D프린터에 들어가는 강력한 모터가 아니기에 충분히 고정이 가능했으며,

생각보다 꽤 강력한 성능을 자랑합니다.

무엇보다 레이저 모듈의 성능이 생각보다 발군이라 놀랬습니다.

(아무래도 레이저를 직접 눈에 쏠경우 시력손상이 예상될것 같아 레이저를 자주 깜박이도록 제작하였습니다.)

 

 

상당히 귀엽게 작동하고 있습니다.

수명은 얼마나 갈지 모르겠으나 일단 계속 돌려볼렵니다. ㅎㅎㅎ

Comments