safwan092

Untitled

Nov 15th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. //✓ - car moving randomly for 60 sec
  2. //✓ - after 50 sec buzzer turnes on for 10 sec
  3. //✓ - when time is 60+ sec car and buzzer turn off
  4. //✓ - main if statment if loud noise withing 60 sec turn of arduino
  5.  
  6.  
  7. int State = LOW;
  8. long previousMillis = 0;
  9.  
  10. long interval = 1000;
  11. long totalTime = 60000;
  12.  
  13. long BuzzerTime = 50000;
  14.  
  15. int t = 548;
  16. int analogValue = 0;
  17.  
  18. #define motor1pin1 2
  19. #define motor1pin2 3
  20. #define motor2pin1 4
  21. #define motor2pin2 5
  22. #define Buzzer 6
  23. #define Sound_Pin A0
  24.  
  25. ///////////////////////////////////////////////////////////////////////////////////////////
  26. void setup() {
  27. initOutputs();
  28. Serial.begin(9600);
  29. }
  30. ///////////////////////////////////////////////////////////////////////////////////////////
  31.  
  32.  
  33. ///////////////////////////////////////////////////////////////////////////////////////////
  34. void loop() {
  35. unsigned long currentMillis = millis();
  36. analogValue = analogRead(Sound_Pin);
  37. //***************************************************
  38. // start of TotalTime
  39. if ((currentMillis < totalTime) && (analogValue < t)) {
  40. if ((currentMillis - previousMillis > interval) && (currentMillis < BuzzerTime)) {
  41. previousMillis = currentMillis;
  42. Serial.println("1st if Statment");
  43. digitalWrite(Buzzer, 0);
  44. moveCarRandomly();
  45. }
  46. else {
  47. previousMillis = currentMillis;
  48. Serial.println("1st else Statment");
  49. digitalWrite(Buzzer, 1);
  50. moveCarRandomly();
  51. }
  52. }//end of TotalTime
  53. //***************************************************
  54. else {
  55. Serial.println("2nd else Statment");
  56. digitalWrite(Buzzer, 0);
  57. Stop();
  58. }
  59. }// end of loop
  60. ///////////////////////////////////////////////////////////////////////////////////////////
  61.  
  62.  
  63. ///////////////////////////////////////////////////////////////////////////////////////////
  64.  
  65. void moveCarRandomly() {
  66. if (State == LOW) {
  67. State = HIGH;
  68. front();
  69. }
  70. else {
  71. State = LOW;
  72. right();
  73. }
  74. }
  75.  
  76. void initOutputs() {
  77. pinMode(Sound_Pin,INPUT);
  78. pinMode(motor1pin1, OUTPUT);
  79. pinMode(motor1pin2, OUTPUT);
  80. pinMode(motor2pin1, OUTPUT);
  81. pinMode(motor2pin2, OUTPUT);
  82. pinMode(Buzzer, OUTPUT);
  83. digitalWrite(motor1pin1, 0);
  84. digitalWrite(motor1pin2, 0);
  85. digitalWrite(motor2pin1, 0);
  86. digitalWrite(motor2pin2, 0);
  87. digitalWrite(Buzzer, 0);
  88. }
  89.  
  90. void front() {
  91. digitalWrite(motor1pin1, HIGH);
  92. digitalWrite(motor1pin2, LOW);
  93.  
  94. digitalWrite(motor2pin1, LOW);
  95. digitalWrite(motor2pin2, HIGH);
  96.  
  97. }
  98.  
  99.  
  100. void right() {
  101. digitalWrite(motor1pin1, LOW);
  102. digitalWrite(motor1pin2, HIGH);
  103.  
  104. digitalWrite(motor2pin1, LOW);
  105. digitalWrite(motor2pin2, HIGH);
  106. }
  107.  
  108.  
  109. void Stop() {
  110. digitalWrite(motor1pin1, LOW);
  111. digitalWrite(motor1pin2, LOW);
  112.  
  113. digitalWrite(motor2pin1, LOW);
  114. digitalWrite(motor2pin2, LOW);
  115. }
Add Comment
Please, Sign In to add comment