Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include "notebook.h";
- #include "themes.h";
- const int input1OfTheDCMotor = 9;
- const int input2OfTheDCMotor = 10;
- const int pwmInputOfTheDCMotor = 6;
- const int buttonToStartStopTheDcMotor = 2;
- const int buttonToShowToChangeTheDirectionOfTheDCMotor = 3;
- const int greenLed = 12;
- const int redLed = 8;
- const int tempSensor = A0;
- const int servoPin = 11;
- Servo servo;
- volatile byte stateOfTheButtonToStartStopTheDCMotor = LOW;
- bool isRunning = LOW;
- bool changeDirection = LOW;
- bool isEngineSoundsPlaying = HIGH;
- void setup() {
- //Leds setup
- pinMode(greenLed, OUTPUT);
- pinMode(redLed, OUTPUT);
- digitalWrite(redLed, HIGH);
- //buttons
- pinMode(buttonToStartStopTheDcMotor, INPUT);
- pinMode(buttonToShowToChangeTheDirectionOfTheDCMotor, INPUT);
- //DC motor
- pinMode(input1OfTheDCMotor, OUTPUT);
- pinMode(input2OfTheDCMotor, OUTPUT);
- digitalWrite(input1OfTheDCMotor, LOW);
- digitalWrite(input2OfTheDCMotor, HIGH);
- //pwm signal
- pinMode(pwmInputOfTheDCMotor, OUTPUT);
- //Servo
- servo.attach(servoPin);
- //interrupts
- attachInterrupt(digitalPinToInterrupt(buttonToStartStopTheDcMotor), readTheChangeToStartStop, RISING );
- attachInterrupt(digitalPinToInterrupt(buttonToShowToChangeTheDirectionOfTheDCMotor), readTheChangeToChangeDirection, RISING );
- }
- void loop() {
- int valueOfTheSensor = analogRead(tempSensor);
- float voltage = (valueOfTheSensor / 1024.0) * 5.0;
- float temp = (voltage -0.5) * 100;
- if (temp >= 20.00)
- isRunning = LOW;
- }
- changeTheStateOfTheLights(isRunning);
- startTheDCMotorAndStopIt(isRunning);
- changeTheDirectionOfTheDCMotor(changeDirection);
- }
- void readTheChangeToStartStop () {
- isRunning = !isRunning;
- changeTheStatusOfTheEnginePlaying();
- }
- void readTheChangeToChangeDirection () {
- changeDirection = !changeDirection;
- }
- void changeTheStatusOfTheEnginePlaying (){
- if (isRunning == HIGH){
- isEngineSoundsPlaying = HIGH;
- } else {
- isEngineSoundsPlaying = LOW;
- }
- }
- void changeTheStateOfTheLights(bool stateOfLight) {
- digitalWrite(greenLed, stateOfLight);
- digitalWrite(redLed, !stateOfLight);
- }
- void startTheDCMotorAndStopIt(bool isRunning){
- if (isRunning == HIGH) {
- digitalWrite(pwmInputOfTheDCMotor, HIGH);
- playCarMusicStart();
- changeToNeutral();
- } else {
- digitalWrite(pwmInputOfTheDCMotor, LOW);
- changeToRedLight();
- }
- }
- void changeTheDirectionOfTheDCMotor(bool changeDirection) {
- digitalWrite(input1OfTheDCMotor, changeDirection);
- digitalWrite(input2OfTheDCMotor, !changeDirection);
- }
- void changeToRedLight() {
- servo.write(0);
- delay(15);
- }
- void changeToNeutral() {
- servo.write(40);
- delay(15);
- }
- void playCarMusicStart(){
- if (isEngineSoundsPlaying == HIGH) {
- for (int thisNote = 0; thisNote < (sizeof(carSounds_note)/sizeof(int)); thisNote++) {
- int noteDuration = 1000 / carsounds_duration[thisNote];
- tone(4, carSounds_note[thisNote], noteDuration);
- int pauseBetweenNotes = noteDuration * 2.70;
- delay(pauseBetweenNotes);
- noTone(4);
- isEngineSoundsPlaying = LOW;
- }
- }
- }
Add Comment
Please, Sign In to add comment