BASIC4MCU | 질문게시판 | 답변 : 답변 : 초음파 센서와 네오픽셀, 서버모터, GROVE MP3
페이지 정보
작성자 master 작성일2018-12-18 22:28 조회2,857회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=7033
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-12-18 오후 10:41:00// by Ok-Hyun Park//#include <SoftwareSerial.h>#include <MP3Player_KT403A.h>SoftwareSerial mp3(2,3);//#include <Adafruit_NeoPixel.h>int PIN=3;Adafruit_NeoPixel strip=Adafruit_NeoPixel(60,PIN,NEO_GRB+NEO_KHZ800);//int echo=7,trig=6;//#include <Servo.h>Servo servo1;//static uint8_t len=0,recv_cmd[20];//unsigned long sonar_time=0,servo_time=0,Neo_time=0;int cnt=0,i,pos=0;;long distance=100;//void setup(){pinMode(trig,OUTPUT); pinMode(echo,INPUT);strip.begin(); strip.show();servo1.attach(9);//mp3.begin(9600);Serial.begin(9600); while(!Serial);Serial.println("Grove-Serial MP3 Demo");Serial.println("Input command: \r\n\r\n""P[]play music by default index\r\n""Pm[]play music in MP3 folder by index\r\n""Pf[][]play music by specify folder and index\r\n""p Pause\r\n""R Resume\r\n""N Next\r\n""L Previous\r\n""l Loop\r\n""I Increase volume\r\n""D Decrease volumern\r\n");delay(100);SelectPlayerDevice(0x02); // Select SD card as the player device.SetVolume(15); // 0~30}//bool flag=0;//void loop(){if(Serial.available()){byte chr=Serial.read(); // Serial.print(chr);if(chr=='\n'){recv_cmd[len]=0;// Print reveiced data// Serial.print("Received cmd: ");// for(i=0; i<len; i++){ Serial.print(recv_cmd[i]); Serial.print(" "); }// Serial.println();switch(recv_cmd[0]){case 'P':{if(recv_cmd[1]=='m'){ /***Play music in "MP3" folder by index*example: "Pm1"->./MP3/0001.mp3*/PlayMP3folder(recv_cmd[2]-'0');Serial.print("Play "); Serial.write(byte(recv_cmd[2])); Serial.println(".mp3 in MP3 folder");}else if(recv_cmd[1]=='f'){ /***Play specify folder and music*example: "Pf11"->./01/001.mp3*/SpecifyfolderPlay(recv_cmd[2]-'0',recv_cmd[3]-'0');Serial.print("Play "); Serial.write(byte(recv_cmd[3])); Serial.print("xxx.mp3");Serial.print(" in folder "); Serial.write(byte(recv_cmd[2])); Serial.println();}else{ /***Play music by default index*example: "P1"->./***.mp3*/SpecifyMusicPlay(recv_cmd[1]-'0');Serial.print("Play xxx.MP3 by index "); Serial.write(byte(recv_cmd[1])); Serial.println();}break;}case 'p': PlayPause(); Serial.println("Pause the MP3 player"); break;case 'R': PlayResume(); Serial.println("Resume the MP3 player"); break;case 'N': PlayNext(); Serial.println("Play the next song"); break;case 'L': PlayPrevious(); Serial.println("Play the previous song"); break;case 'l': PlayLoop(); Serial.println("Play loop for all the songs"); break;case 'I': IncreaseVolume(); Serial.println("Increase volume"); break;case 'D': DecreaseVolume(); Serial.println("Decrease volume"); break;}//for(i=0; i<8; i++)recv_cmd[i]=0; // clean data bufferlen=0;}else{ recv_cmd[len++]=chr; }}//-------------------------------------------------------------------if(millis()-sonar_time>=100){ sonar_time=millis();digitalWrite(trig,1); delayMicroseconds(10); digitalWrite(trig,0);distance=pulseIn(echo,HIGH)/58;Serial.print(distance); Serial.println(" Cm");if(distance<30){if(flag==0){ flag=1; PlayMP3folder(1); }}else{if(flag==1){ flag=0; PlayPause(); }}}//if(millis()-servo_time>=1000){ servo_time=millis();if(distance<30){ if(pos==0)pos=180; else pos=0; servo1.write(pos); }}//if(millis()-Neo_time>=10){ Neo_time=millis(); // 네오픽셀 딜레이 10msif(distance<30){if(cnt<256)i=cnt;else if(cnt<512)i=511-cnt;if(++cnt>811)cnt=0;//if(cnt<512){strip.setPixelColor(0,strip.Color(0,50,255));strip.setPixelColor(1,strip.Color(0,100,255));strip.setPixelColor(2,strip.Color(0,150,255));strip.setPixelColor(3,strip.Color(0,200,255));strip.setPixelColor(4,strip.Color(0,255,255));strip.setPixelColor(5,strip.Color(i,255-i,255-i));strip.setPixelColor(6,strip.Color(i,255,255-i));strip.setPixelColor(7,strip.Color(0,255,255)); //초록strip.setPixelColor(8,strip.Color(255,255,0)); //노랑strip.show();}}else{ // if(distance>30)cnt=0;strip.setPixelColor(0,strip.Color(255,50,0));strip.setPixelColor(1,strip.Color(255,100,0));strip.setPixelColor(2,strip.Color(255,150,0));strip.setPixelColor(3,strip.Color(255,200,0));strip.setPixelColor(4,strip.Color(255,255,0));strip.setPixelColor(5,strip.Color(255,255,0));strip.setPixelColor(6,strip.Color(255,255,0));strip.setPixelColor(7,strip.Color(255,255,0)); //초록strip.setPixelColor(8,strip.Color(255,255,0)); //노랑strip.show();}}}
댓글 0
조회수 2,857등록된 댓글이 없습니다.