Components and supplies
Pushbutton
Gear motor
IR Sensors
Arduino Mega 2560
4wd car chassis
Bread board
LED (generic)
nRF24L01 tranceiver
18650 Lion battery
L298N Motor Driver Dual H-Bridge
6v mini water pump
Arduino Nano
Tools and machines
Screw drivers (various)
Apps and platforms
arduino ide
Proteus 8
Project description
Code
Code
Arduino Mega
cpp
1#include <SPI.h> // Include SPI library 2#include <nRF24L01.h> // Include nRF24L01 library 3#include <RF24.h> // Include RF24 library 4#include <Servo.h> // Include Servo Library 5 6Servo servoMotor; 7 8RF24 radio(2, 3); 9const byte address[6] = "00001"; // Address for the communication channel 10 11 12#define LFM 9 // Left forward Motor 13#define LBM 8 // Left backward Motor 14#define RFM 11 // Right forward Motor 15#define RBM 10 // Right backward Motor 16#define LPWM 12 // Left PWM 17#define RPWM 13 // Right PWM 18 19 20#define LeftLED 22 21#define RightLED 23 22#define ReverseLED 24 23#define FlameLed 25 // flame detect led 24#define PumpLED 26 // pumpled 25#define SLED 27 // Left servo 26#define RSLED 28 // Right servo 27#define WPLED 29 // Water pump 28#define SignalLED 30 29 30#define servo 5 31 32#define WPump 7 // Water pump pin 33#define FlameSensor 4 // IR sensor pin 34 35int angle = 90; // Servo angle 36int pumpstate = 1; // Water pump state 37 38unsigned long currentTime; // to blink light 39unsigned long previousTime = 0; 40const long interval = 200; 41int ledStateblink = LOW; 42int reset = 0; 43 44 45void setup() { 46 47 48 radio.begin(); 49 radio.openReadingPipe(1, address); 50 radio.setPALevel(RF24_PA_LOW); 51 radio.startListening(); 52 53 54 servoMotor.attach(servo); 55 servoMotor.write(90); 56 57 for (int x = 22; x < 31; x++) { // LED OUTPUT 58 59 pinMode(x, OUTPUT); 60 } 61 62 63 64 pinMode(LFM, OUTPUT); 65 pinMode(LBM, OUTPUT); 66 pinMode(RFM, OUTPUT); 67 pinMode(RBM, OUTPUT); 68 pinMode(LPWM, OUTPUT); 69 pinMode(RPWM, OUTPUT); 70 pinMode(WPump, OUTPUT); 71 pinMode(FlameSensor, INPUT); 72 73 servoMotor.write(angle); 74 analogWrite(LPWM, 150); 75 analogWrite(RPWM, 155); 76 digitalWrite(WPump, HIGH); 77 78 Serial.begin(9600); // Initialize serial communication 79 SPI.begin(); // Initialize SPI communication 80 81} 82 83 84 85void loop() { 86 87 88 89 90 if (reset == 0) { 91 if (digitalRead(FlameSensor) == 0) { 92 digitalWrite(FlameLed, HIGH); 93 if (pumpstate == 0) { 94 digitalWrite(WPump, LOW); 95 digitalWrite(PumpLED, HIGH); 96 } 97 } 98 } 99 100 101 if (radio.available()) { 102 char remoteData; 103 radio.read(&remoteData, sizeof(remoteData)); 104 105 106 if (remoteData == 'F') { 107 108 forward(); // Go Forward 109 digitalWrite(LeftLED, HIGH); 110 digitalWrite(RightLED, HIGH); 111 digitalWrite(ReverseLED, LOW); 112 digitalWrite(SignalLED, HIGH); 113 } 114 115 else if (remoteData == 'B') { 116 117 backward(); //Go Backword 118 digitalWrite(LeftLED, LOW); 119 digitalWrite(RightLED, LOW); 120 digitalWrite(ReverseLED, HIGH); 121 digitalWrite(SignalLED, HIGH); 122 } 123 124 else if (remoteData == 'L') { 125 126 turnLeft(); //Turn Left 127 digitalWrite(LeftLED, HIGH); 128 digitalWrite(RightLED, LOW); 129 digitalWrite(ReverseLED, LOW); 130 digitalWrite(SignalLED, HIGH); 131 } 132 133 else if (remoteData == 'R') { 134 135 turnRight(); //Turn Right 136 digitalWrite(LeftLED, LOW); 137 digitalWrite(RightLED, HIGH); 138 digitalWrite(ReverseLED, LOW); 139 digitalWrite(SignalLED, HIGH); 140 141 142 } else if (remoteData == 'A') { 143 144 digitalWrite(SignalLED, HIGH); 145 digitalWrite(SLED, HIGH); 146 if (angle >= 0) { 147 angle--; 148 delay(10); 149 servoMotor.write(angle); 150 } 151 152 } 153 154 else if (remoteData == 'D') { 155 156 157 digitalWrite(RSLED, HIGH); 158 159 digitalWrite(SignalLED, HIGH); 160 161 if (angle <= 180) { // Ensure angle doesn't exceed servo's maximum angle 162 angle++; 163 delay(10); 164 servoMotor.write(angle); 165 } 166 167 168 } 169 170 else if (remoteData == 'Y') { //to reset state of fire detecting 171 172 reset = 0; 173 174 175 digitalWrite(FlameLed, LOW); 176 177 178 } 179 180 181 182 else if (remoteData == 'S') { 183 184 stop(); 185 digitalWrite(LeftLED, LOW); 186 digitalWrite(RightLED, LOW); 187 digitalWrite(SignalLED, LOW); 188 currentTime = millis(); //Light Blinking 189 if (currentTime - previousTime >= interval) { 190 previousTime = currentTime; 191 if (ledStateblink == LOW) { 192 ledStateblink = HIGH; 193 } else { 194 ledStateblink = LOW; 195 } 196 digitalWrite(ReverseLED, ledStateblink); 197 digitalWrite(SignalLED, ledStateblink); 198 } 199 200 } else if (remoteData == 'W') { 201 202 pumpstate = !pumpstate; 203 204 digitalWrite(SignalLED, HIGH); 205 delay(50); 206 207 digitalWrite(SLED, HIGH); 208 digitalWrite(PumpLED, pumpstate); 209 digitalWrite(WPump, !pumpstate); 210 211 return; 212 } 213 } 214} 215 216 217 218 219 220 221// Motor controller functions 222void forward() { 223 digitalWrite(LFM, HIGH); 224 digitalWrite(RFM, HIGH); 225 digitalWrite(LBM, LOW); 226 digitalWrite(RBM, LOW); 227} 228 229void backward() { 230 digitalWrite(LFM, LOW); 231 digitalWrite(RFM, LOW); 232 digitalWrite(LBM, HIGH); 233 digitalWrite(RBM, HIGH); 234} 235 236void turnLeft() { 237 digitalWrite(LFM, LOW); 238 digitalWrite(RFM, HIGH); 239 digitalWrite(LBM, HIGH); 240 digitalWrite(RBM, LOW); 241} 242 243void turnRight() { 244 digitalWrite(LFM, HIGH); 245 digitalWrite(RFM, LOW); 246 digitalWrite(LBM, LOW); 247 digitalWrite(RBM, HIGH); 248} 249 250void stop() { 251 digitalWrite(LFM, LOW); 252 digitalWrite(RFM, LOW); 253 digitalWrite(LBM, LOW); 254 digitalWrite(RBM, LOW); 255}
Arduino Nano
1#include <SPI.h> // Include SPI library 2#include <nRF24L01.h> // Include nRF24L01 library 3#include <RF24.h> // Include RF24 library 4 5RF24 radio(9, 10); // Define RF24 object with CE, CSN pins 6const byte address[6] = "00001"; // Address for the communication channel 7 8#define FSwitch 6 // directions switches 9#define BSwitch 7 10#define RSwitch 5 11#define LSwitch 4 12 13#define Lservosw 0 // servo switches 14#define Rservosw 1 15 16#define pumpsw 8 // for pump switch 17 18#define LED A2 // for LED 19#define LED2 A3 // for another LED 20 21void setup() { 22 radio.begin(); // Initialize radio 23 radio.openWritingPipe(address); // Open writing pipe 24 radio.openReadingPipe(1, address); // Open reading pipe 25 radio.setPALevel(RF24_PA_LOW); // Set power level 26 27 // Set pin modes 28 pinMode(FSwitch, INPUT_PULLUP); 29 pinMode(BSwitch, INPUT_PULLUP); 30 pinMode(LSwitch, INPUT_PULLUP); 31 pinMode(RSwitch, INPUT_PULLUP); 32 pinMode(Lservosw, INPUT_PULLUP); 33 pinMode(Rservosw, INPUT_PULLUP); 34 pinMode(pumpsw, INPUT_PULLUP); 35 pinMode(LED2, OUTPUT); 36 37 Serial.begin(9600); // Initialize serial communication 38} 39 40void loop() { 41 // Read state of switches 42 int Forward = !digitalRead(FSwitch); 43 int Backward = !digitalRead(BSwitch); 44 int Left = !digitalRead(LSwitch); 45 int Right = !digitalRead(RSwitch); 46 int LServo = !digitalRead(Lservosw); 47 int Rservo = !digitalRead(Rservosw); 48 int pump = !digitalRead(pumpsw); 49 50 // Count how many switches are pressed 51 int buttonCount = Forward + Backward + Left + Right + LServo + Rservo + pump; 52 53 // Check if both servo switches are pressed 54 if (LServo == HIGH && Rservo == HIGH) { 55 char dataToSend = 'Y'; 56 analogWrite(LED, 255); 57 radio.write(&dataToSend, sizeof(dataToSend)); 58 return; // 59 } 60 // Check if more than one switch is pressed 61 else if (buttonCount >= 2) { 62 char dataToSend = 'S'; 63 analogWrite(LED, 0); 64 radio.write(&dataToSend, sizeof(dataToSend)); 65 delay(100); // Delay for stability 66 return; 67 } 68 69 // Check which direction switch is pressed and send corresponding signal 70 if (Forward == HIGH) { 71 char dataToSend = 'F'; // move forward 72 analogWrite(LED, 255); 73 radio.write(&dataToSend, sizeof(dataToSend)); 74 } else if (Backward == HIGH) { 75 char dataToSend = 'B'; // move backward 76 analogWrite(LED, 255); 77 radio.write(&dataToSend, sizeof(dataToSend)); 78 } else if (Left == HIGH) { 79 char dataToSend = 'L'; // turn left 80 analogWrite(LED, 255); 81 radio.write(&dataToSend, sizeof(dataToSend)); 82 } else if (Right == HIGH) { 83 char dataToSend = 'R'; // turn right 84 analogWrite(LED, 255); 85 radio.write(&dataToSend, sizeof(dataToSend)); 86 } else if (LServo == HIGH) { 87 char dataToSend = 'A'; // move left servo 88 analogWrite(LED, 255); 89 radio.write(&dataToSend, sizeof(dataToSend)); 90 } else if (Rservo == HIGH) { 91 char dataToSend = 'D'; // move right servo 92 analogWrite(LED, 255); 93 radio.write(&dataToSend, sizeof(dataToSend)); 94 } else if (pump == HIGH) { 95 char dataToSend = 'W'; // operate pump 96 delay(500); // Delay for stability 97 analogWrite(LED, 255); 98 radio.write(&dataToSend, sizeof(dataToSend)); 99 } else if (Right == LOW && Right == LOW && Right == LOW && Right == LOW) { 100 char dataToSend = 'S'; 101 analogWrite(LED, 0); 102 radio.write(&dataToSend, sizeof(dataToSend)); 103 } else { 104 char dataToSend = 'S'; 105 analogWrite(LED, 0); 106 radio.write(&dataToSend, sizeof(dataToSend)); 107 } 108}
Downloadable files
Arduino Mega Code
CARMEGA.ino
Arduino Nano Code
Remote.ino
Comments
Only logged in users can leave comments
pipisara_c
0 Followers
•0 Projects
Table of contents
Intro
3
0