Eng-Mohamed-Essam

Untitled

Aug 16th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<LiquidCrystal.h> // Including the LCD Library
  2. #include<Keypad.h> // Including the Keypad Library
  3. const byte Rows = 4; // Defining the number of Rows
  4. const byte columns = 3; // Defining the number of Columns
  5. char hexakeys[Rows][columns] = {
  6.   {'1','2','3'},
  7.   {'4','5','6'},
  8.   {'7','8','9'},
  9.   {'*','0','#'}
  10. }; // Defining the Symbols on the keybad
  11. byte rowpins [Rows] = {22, 23, 24, 25}; // Defining pins for the Rows
  12. byte colpins [columns] = {26, 27, 28}; // Defining pins for the columns
  13. LiquidCrystal lcd(13,12,11,10,9,8); // Defining the pins for the LCD Char screen
  14. Keypad CustomKeyPad = Keypad(makeKeymap(hexakeys), rowpins, colpins, Rows, columns);
  15. int forwardpin = 7; //pin For moving Forward
  16. int backwardpin = 6; //pin For moving Backwards
  17. int rotateRightpin = 5; //pin For rotating right
  18. int rotateLeftpin = 4; //pin For rotating Left
  19. int F = 0, B = 0, R = 0, L = 0; //Vars taking input val
  20. int mode = 3; //pin For Mode (Auto, Manual)
  21. int applyspeed = 21; //pin For ApplyingSpeed
  22. int M, A; //Vars for input val of PB
  23. int StateHolder = 0;
  24. byte Arr[3] = {0,0,0}; // declaring an array to hold the CustomKey
  25. int Counter = 0; // Counter for the array
  26. int GetSpeed(){ // Array that Gives me the value of the speed in integer of 3 digits
  27.   int Speed = Arr[2];
  28.   Speed = Speed + Arr[1] * 10;
  29.   Speed = Speed + Arr[0] * 100;
  30.   return Speed;
  31.   }
  32. void setup() {
  33.   // put your setup code here, to run once:
  34.   pinMode (forwardpin, INPUT);
  35.   pinMode (backwardpin, INPUT);
  36.   pinMode (rotateRightpin, INPUT);
  37.   pinMode (rotateLeftpin, INPUT);
  38.   pinMode (mode, INPUT);
  39.   pinMode (applyspeed, INPUT);
  40.   lcd.begin(16,2);
  41.   lcd.print("Speed: "); // Prnting the word "speed:" on the first line
  42.   Serial.begin(9600); //Defining the frequency (bits sent per second
  43.  
  44. }
  45.  
  46. void loop() {
  47.   // put your main code here, to run repeatedly:
  48.   F = digitalRead (forwardpin); //Read the F value
  49.   if (F == HIGH){
  50.     Serial.print('1'); // Sending 1 to serial out if F = high
  51.   }
  52.   else {
  53.     Serial.print("10"); // Note : If serial Transfer doesn't support String , Replace with Char
  54.   }
  55.   B = digitalRead (backwardpin); //Read the B value
  56.   if (B == HIGH){
  57.     Serial.print('2'); // Sending 2 to serial out if B = high
  58.   }
  59.   else {
  60.     Serial.print("20");
  61.   }
  62.   R = digitalRead (rotateRightpin); //Read the R value
  63.   if (R == HIGH){
  64.     Serial.print('3');
  65.   }
  66.   else {
  67.     Serial.print("30");
  68.   }
  69.   L = digitalRead (rotateLeftpin); //Read the L value
  70.   if (L == HIGH){
  71.     Serial.print('4');
  72.   }
  73.   else {
  74.     Serial.print("40");
  75.   }
  76.   M = digitalRead (mode);//Read the mode PB value
  77.   if (M == HIGH){
  78.     Serial.print('5');
  79.   }
  80.   else {
  81.     Serial.print("50");
  82.   }
  83.   A = digitalRead (applyspeed);//Read the ApplySpeed PB value
  84.   if (A == HIGH){
  85.     Serial.print('6');
  86.   }
  87.   else {
  88.     Serial.print("60");
  89.   }
  90.   char CustomKey = CustomKeyPad.getKey(); // Getting input from the keypad
  91.   if (CustomKey >= '0' || CustomKey <= '9'){
  92.     Arr[Counter] = CustomKey - '0'; // Converting to Integer
  93.     Counter++;
  94.     if (Counter == 3){ // if Counter reaches the limit of 3 it returns to zero
  95.       Counter = 0;
  96.     }
  97.   }
  98.     if (A == HIGH){
  99.       StateHolder = HIGH;
  100.   }
  101.   if (StateHolder){ // if we press applyspeed button pressed excute the following code
  102.     Serial.print('7');
  103.     Serial.print(GetSpeed()); // Getting the speed in integer form
  104.     lcd.clear(); // Clearing the LCD screen from old values
  105.     lcd.setCursor(0, 1); // Setting the curser on the next line
  106.     lcd.print(CustomKey); // Displaying the input value from the KeyPad on the LCD screen
  107.   }
  108.   else {
  109.     lcd.clear();
  110.   }
  111.   if (M == LOW && StateHolder == HIGH){ // if the Mode is set low that activates the manual control
  112.     if (CustomKey && F){
  113.       lcd.clear();
  114.       lcd.setCursor(0, 1);
  115.       lcd.print('F');
  116.       lcd.print(CustomKey);
  117.     }
  118.     else if (CustomKey && B){
  119.       lcd.clear();
  120.       lcd.setCursor(0, 1);
  121.       lcd.print('B');
  122.       lcd.print(CustomKey);  
  123.     }
  124.     else if (CustomKey && R){
  125.       lcd.clear();
  126.       lcd.setCursor(0, 1);
  127.       lcd.print('R');  
  128.       lcd.print(CustomKey);  
  129.     }
  130.     else if (CustomKey && L){
  131.       lcd.clear();
  132.       lcd.setCursor(0, 1);
  133.       lcd.print('L');
  134.       lcd.print(CustomKey);  
  135.     }
  136.     else {
  137.       lcd.clear();  
  138.     }
  139.   }
  140.   else if (M == HIGH){ // When mode is high Automatic mode is on
  141.       lcd.clear();
  142.       lcd.setCursor(0, 1);
  143.       lcd.print("Autonomus");
  144.       StateHolder = LOW;      
  145.   }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment