Advertisement
safwan092

Untitled

Nov 15th, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 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. }
  49.  
  50. //***************************************************
  51. // start of TotalTime
  52. if ((currentMillis < totalTime) && (analogValue < t) && (flag == 0)) {
  53. if (currentMillis - previousMillis >= interval) {
  54. previousMillis = currentMillis;
  55. Serial.println("3rd if Statment");
  56. digitalWrite(Buzzer, 0);
  57. moveCarRandomly();
  58. }
  59. else {
  60. previousMillis = currentMillis;
  61. Serial.println("1st else Statment");
  62. digitalWrite(Buzzer, 0);
  63. moveCarRandomly();
  64. }
  65. }//end of TotalTime
  66. //***************************************************
  67.  
  68. else {
  69. Serial.println("2nd else Statment");
  70. flag = 1;
  71. digitalWrite(Buzzer, 0);
  72. Stop();
  73. }
  74. }// end of loop
  75. ///////////////////////////////////////////////////////////////////////////////////////////
  76.  
  77.  
  78. ///////////////////////////////////////////////////////////////////////////////////////////
  79.  
  80. void moveCarRandomly() {
  81. if (State == LOW) {
  82. State = HIGH;
  83. front();
  84. delay(500);
  85. }
  86. else {
  87. State = LOW;
  88. right();
  89. delay(500);
  90. }
  91. }
  92.  
  93. void initOutputs() {
  94. pinMode(Sound_Pin, INPUT);
  95. pinMode(motor1pin1, OUTPUT);
  96. pinMode(motor1pin2, OUTPUT);
  97. pinMode(motor2pin1, OUTPUT);
  98. pinMode(motor2pin2, OUTPUT);
  99. pinMode(Buzzer, OUTPUT);
  100. digitalWrite(motor1pin1, 0);
  101. digitalWrite(motor1pin2, 0);
  102. digitalWrite(motor2pin1, 0);
  103. digitalWrite(motor2pin2, 0);
  104. digitalWrite(Buzzer, 0);
  105. }
  106.  
  107. void front() {
  108. digitalWrite(motor1pin1, HIGH);
  109. digitalWrite(motor1pin2, LOW);
  110.  
  111. digitalWrite(motor2pin1, LOW);
  112. digitalWrite(motor2pin2, HIGH);
  113.  
  114. }
  115.  
  116.  
  117. void right() {
  118. digitalWrite(motor1pin1, LOW);
  119. digitalWrite(motor1pin2, HIGH);
  120.  
  121. digitalWrite(motor2pin1, LOW);
  122. digitalWrite(motor2pin2, HIGH);
  123. }
  124.  
  125.  
  126. void Stop() {
  127. digitalWrite(motor1pin1, LOW);
  128. digitalWrite(motor1pin2, LOW);
  129.  
  130. digitalWrite(motor2pin1, LOW);
  131. digitalWrite(motor2pin2, LOW);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement