Guest User

Untitled

a guest
Apr 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include <Servo.h>
  2. #include "notebook.h";
  3. #include "themes.h";
  4.  
  5. const int input1OfTheDCMotor = 9;
  6. const int input2OfTheDCMotor = 10;
  7. const int pwmInputOfTheDCMotor = 6;
  8. const int buttonToStartStopTheDcMotor = 2;
  9. const int buttonToShowToChangeTheDirectionOfTheDCMotor = 3;
  10. const int greenLed = 12;
  11. const int redLed = 8;
  12. const int tempSensor = A0;
  13. const int servoPin = 11;
  14. Servo servo;
  15.  
  16.  
  17. volatile byte stateOfTheButtonToStartStopTheDCMotor = LOW;
  18. bool isRunning = LOW;
  19. bool changeDirection = LOW;
  20. bool isEngineSoundsPlaying = HIGH;
  21.  
  22. void setup() {
  23. //Leds setup
  24. pinMode(greenLed, OUTPUT);
  25. pinMode(redLed, OUTPUT);
  26. digitalWrite(redLed, HIGH);
  27. //buttons
  28. pinMode(buttonToStartStopTheDcMotor, INPUT);
  29. pinMode(buttonToShowToChangeTheDirectionOfTheDCMotor, INPUT);
  30. //DC motor
  31. pinMode(input1OfTheDCMotor, OUTPUT);
  32. pinMode(input2OfTheDCMotor, OUTPUT);
  33. digitalWrite(input1OfTheDCMotor, LOW);
  34. digitalWrite(input2OfTheDCMotor, HIGH);
  35. //pwm signal
  36. pinMode(pwmInputOfTheDCMotor, OUTPUT);
  37. //Servo
  38. servo.attach(servoPin);
  39. //interrupts
  40. attachInterrupt(digitalPinToInterrupt(buttonToStartStopTheDcMotor), readTheChangeToStartStop, RISING );
  41. attachInterrupt(digitalPinToInterrupt(buttonToShowToChangeTheDirectionOfTheDCMotor), readTheChangeToChangeDirection, RISING );
  42. }
  43.  
  44. void loop() {
  45. int valueOfTheSensor = analogRead(tempSensor);
  46. float voltage = (valueOfTheSensor / 1024.0) * 5.0;
  47. float temp = (voltage -0.5) * 100;
  48. if (temp >= 20.00)
  49. isRunning = LOW;
  50. }
  51. changeTheStateOfTheLights(isRunning);
  52. startTheDCMotorAndStopIt(isRunning);
  53. changeTheDirectionOfTheDCMotor(changeDirection);
  54. }
  55.  
  56. void readTheChangeToStartStop () {
  57. isRunning = !isRunning;
  58. changeTheStatusOfTheEnginePlaying();
  59. }
  60.  
  61. void readTheChangeToChangeDirection () {
  62. changeDirection = !changeDirection;
  63. }
  64.  
  65. void changeTheStatusOfTheEnginePlaying (){
  66. if (isRunning == HIGH){
  67. isEngineSoundsPlaying = HIGH;
  68. } else {
  69. isEngineSoundsPlaying = LOW;
  70. }
  71. }
  72.  
  73. void changeTheStateOfTheLights(bool stateOfLight) {
  74. digitalWrite(greenLed, stateOfLight);
  75. digitalWrite(redLed, !stateOfLight);
  76. }
  77.  
  78. void startTheDCMotorAndStopIt(bool isRunning){
  79. if (isRunning == HIGH) {
  80. digitalWrite(pwmInputOfTheDCMotor, HIGH);
  81. playCarMusicStart();
  82. changeToNeutral();
  83. } else {
  84. digitalWrite(pwmInputOfTheDCMotor, LOW);
  85. changeToRedLight();
  86. }
  87. }
  88.  
  89. void changeTheDirectionOfTheDCMotor(bool changeDirection) {
  90. digitalWrite(input1OfTheDCMotor, changeDirection);
  91. digitalWrite(input2OfTheDCMotor, !changeDirection);
  92. }
  93.  
  94. void changeToRedLight() {
  95. servo.write(0);
  96. delay(15);
  97. }
  98.  
  99. void changeToNeutral() {
  100. servo.write(40);
  101. delay(15);
  102. }
  103.  
  104. void playCarMusicStart(){
  105. if (isEngineSoundsPlaying == HIGH) {
  106. for (int thisNote = 0; thisNote < (sizeof(carSounds_note)/sizeof(int)); thisNote++) {
  107. int noteDuration = 1000 / carsounds_duration[thisNote];
  108. tone(4, carSounds_note[thisNote], noteDuration);
  109. int pauseBetweenNotes = noteDuration * 2.70;
  110. delay(pauseBetweenNotes);
  111. noTone(4);
  112. isEngineSoundsPlaying = LOW;
  113. }
  114. }
  115. }
Add Comment
Please, Sign In to add comment