Guest User

Untitled

a guest
Jul 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <TimerOne.h>
  2.  
  3. int a = 0;
  4. int spd=500; //0~511の値にする
  5.  
  6. void setup(){
  7. Serial.begin(9600);
  8. Timer1.initialize(500000);// initialize timer1, and set a 1/2 second period
  9. Timer1.pwm(9, 0); // setup pwm on pin 9, 50% duty cycle
  10. Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
  11. pinMode(1,OUTPUT); //信号用ピン
  12. pinMode(2,OUTPUT); //信号用ピン
  13. motor_stop();
  14. }
  15.  
  16. void callback(){
  17. if(Serial.available()>0){//データの読み込み
  18. a=Serial.read();
  19. }
  20. }
  21.  
  22. void test_LED(){
  23. digitalWrite(13,HIGH);
  24. delay(500);
  25. digitalWrite(13,LOW);
  26. }
  27.  
  28. void motor_stop(){
  29. digitalWrite(1,LOW);
  30. digitalWrite(2,LOW);
  31. Timer1.pwm(9,512); // setup pwm on pin 9, 50% duty cycle
  32. test_LED();
  33. }
  34.  
  35. void motor_go(){
  36. digitalWrite(1,LOW);
  37. digitalWrite(2,HIGH);
  38. Timer1.pwm(9,512); // setup pwm on pin 9, 50% duty cycle
  39. test_LED();
  40. }
  41.  
  42. void motor_go2(){
  43. digitalWrite(1,LOW);
  44. digitalWrite(2,HIGH);
  45. Timer1.pwm(9,512); // setup pwm on pin 9, 50% duty cycle);
  46. test_LED();
  47. delay(10);
  48. test_LED();
  49. }
  50.  
  51. void loop(){
  52. if(a!=0){
  53. switch(a){
  54. case 'a':
  55. motor_go();
  56. delay(1000);
  57. break;
  58.  
  59. case 'b':
  60. motor_go2();
  61. delay(1000);
  62. break;
  63. }
  64. }
  65. Timer1.pwm(9,0); // setup pwm on pin 9, 50% duty cycle);
  66. }
Add Comment
Please, Sign In to add comment