Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TimerOne.h>
- int a = 0;
- int spd=500; //0~511の値にする
- void setup(){
- Serial.begin(9600);
- Timer1.initialize(500000);// initialize timer1, and set a 1/2 second period
- Timer1.pwm(9, 0); // setup pwm on pin 9, 50% duty cycle
- Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
- pinMode(1,OUTPUT); //信号用ピン
- pinMode(2,OUTPUT); //信号用ピン
- motor_stop();
- }
- void callback(){
- if(Serial.available()>0){//データの読み込み
- a=Serial.read();
- }
- }
- void test_LED(){
- digitalWrite(13,HIGH);
- delay(500);
- digitalWrite(13,LOW);
- }
- void motor_stop(){
- digitalWrite(1,LOW);
- digitalWrite(2,LOW);
- Timer1.pwm(9,512); // setup pwm on pin 9, 50% duty cycle
- test_LED();
- }
- void motor_go(){
- digitalWrite(1,LOW);
- digitalWrite(2,HIGH);
- Timer1.pwm(9,512); // setup pwm on pin 9, 50% duty cycle
- test_LED();
- }
- void motor_go2(){
- digitalWrite(1,LOW);
- digitalWrite(2,HIGH);
- Timer1.pwm(9,512); // setup pwm on pin 9, 50% duty cycle);
- test_LED();
- delay(10);
- test_LED();
- }
- void loop(){
- if(a!=0){
- switch(a){
- case 'a':
- motor_go();
- delay(1000);
- break;
- case 'b':
- motor_go2();
- delay(1000);
- break;
- }
- }
- Timer1.pwm(9,0); // setup pwm on pin 9, 50% duty cycle);
- }
Add Comment
Please, Sign In to add comment