Advertisement
safwan092

Untitled

Nov 15th, 2022
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 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 = 40000;
  14.  
  15. int t = 548;
  16. int analogValue = 0;
  17. int flag = 0;
  18.  
  19. #define motor1pin1 2
  20. #define motor1pin2 3
  21. #define motor2pin1 4
  22. #define motor2pin2 5
  23. #define Buzzer 6
  24. #define Sound_Pin A0
  25.  
  26. ///////////////////////////////////////////////////////////////////////////////////////////
  27. void setup() {
  28. flag = 0;
  29. initOutputs();
  30. Serial.begin(9600);
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////////////////
  33.  
  34.  
  35. ///////////////////////////////////////////////////////////////////////////////////////////
  36. void loop() {
  37. unsigned long currentMillis = millis();
  38. analogValue = analogRead(Sound_Pin);
  39.  
  40. if ((currentMillis >= totalTime) && flag == 0) {
  41. Serial.println("1st if Statment");
  42. flag = 1;
  43. }
  44.  
  45. if ((currentMillis >= BuzzerTime) && flag == 0) {
  46. Serial.println("2nd if Statment");
  47. digitalWrite(Buzzer, 1);
  48. analogValue = t - 10;
  49. }
  50.  
  51. //***************************************************
  52. // start of TotalTime
  53. if ((currentMillis < totalTime) && (analogValue < t) && (flag == 0)) {
  54. if (currentMillis - previousMillis >= interval) {
  55. previousMillis = currentMillis;
  56. Serial.println("3rd if Statment");
  57. digitalWrite(Buzzer, 0);
  58. moveCarRandomly();
  59. }
  60. else {
  61. previousMillis = currentMillis;
  62. Serial.println("1st else Statment");
  63. digitalWrite(Buzzer, 0);
  64. moveCarRandomly();
  65. }
  66. }//end of TotalTime
  67. //***************************************************
  68.  
  69. else {
  70. Serial.println("2nd else Statment");
  71. flag = 1;
  72. digitalWrite(Buzzer, 0);
  73. Stop();
  74. }
  75. }// end of loop
  76. ///////////////////////////////////////////////////////////////////////////////////////////
  77.  
  78.  
  79. ///////////////////////////////////////////////////////////////////////////////////////////
  80.  
  81. void moveCarRandomly() {
  82. if (State == LOW) {
  83. State = HIGH;
  84. front();
  85. delay(500);
  86. }
  87. else {
  88. State = LOW;
  89. right();
  90. delay(500);
  91. }
  92. }
  93.  
  94. void initOutputs() {
  95. pinMode(Sound_Pin, INPUT);
  96. pinMode(motor1pin1, OUTPUT);
  97. pinMode(motor1pin2, OUTPUT);
  98. pinMode(motor2pin1, OUTPUT);
  99. pinMode(motor2pin2, OUTPUT);
  100. pinMode(Buzzer, OUTPUT);
  101. digitalWrite(motor1pin1, 0);
  102. digitalWrite(motor1pin2, 0);
  103. digitalWrite(motor2pin1, 0);
  104. digitalWrite(motor2pin2, 0);
  105. digitalWrite(Buzzer, 0);
  106. }
  107.  
  108. void front() {
  109. digitalWrite(motor1pin1, HIGH);
  110. digitalWrite(motor1pin2, LOW);
  111.  
  112. digitalWrite(motor2pin1, LOW);
  113. digitalWrite(motor2pin2, HIGH);
  114.  
  115. }
  116.  
  117.  
  118. void right() {
  119. digitalWrite(motor1pin1, LOW);
  120. digitalWrite(motor1pin2, HIGH);
  121.  
  122. digitalWrite(motor2pin1, LOW);
  123. digitalWrite(motor2pin2, HIGH);
  124. }
  125.  
  126.  
  127. void Stop() {
  128. digitalWrite(motor1pin1, LOW);
  129. digitalWrite(motor1pin2, LOW);
  130.  
  131. digitalWrite(motor2pin1, LOW);
  132. digitalWrite(motor2pin2, LOW);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement