Advertisement
Bukisoh

Vélo électrique v7

May 28th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. #include <vesc_uart.h>
  5. #include <datatypes.h>
  6.  
  7.  
  8.  
  9.  LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  10.  Servo esc;
  11.  
  12. char Str1[20];
  13. char Str2[21];
  14. char Str3[21];
  15. char Str4[21];
  16.  
  17. bool etat=false;
  18. int chrono=0;
  19. int temps=0;
  20. double vitesse=0;
  21.  
  22. int hallSensorPin=2;
  23. volatile int compteur=0;
  24. char str_vitesse[10];
  25.  
  26. void setup() {
  27.   Serial.begin(115200);
  28.   lcd.begin(20,4);
  29.   esc.attach(9);
  30.  
  31.   pinMode(hallSensorPin, INPUT);                    
  32.   attachInterrupt(0,detectaimant,LOW);
  33.  
  34. }
  35. struct bldcMeasure measuredValues;
  36. void loop() {
  37.  
  38. if (VescUartGetValue(measuredValues)) {
  39.     Serial.print("Loop: ");
  40.     SerialPrint(measuredValues);
  41.   }
  42.   else
  43.   {
  44.     Serial.println("Failed to get data!");
  45.   }
  46.  
  47.    
  48.  chrono=millis();
  49.    if (etat == true) {
  50.      etat=false;
  51.      
  52.      chrono = chrono - temps;
  53.      vitesse =(2/(chrono/1000.0))*3.6;
  54.      delay(5);
  55.      temps=millis();
  56.      
  57.      Serial.println(vitesse);
  58.      dtostrf(vitesse, 4, 2, str_vitesse);
  59.    }
  60.  
  61.   sprintf(Str1, "Vitesse: %s km/h", str_vitesse);
  62.    lcd.setCursor(0,0);
  63.    lcd.print(Str1);
  64.  
  65.   sprintf(Str3, "Throttle: %3d %", map(analogRead(A1),0,1020,0,100));
  66.     lcd.setCursor(0,1);
  67.     lcd.print(Str3);
  68. }
  69.  
  70. float PWM()
  71. {
  72.    int PWMa = analogRead(A1);
  73.        PWMa = map(PWM, 0, 1024, 0, 179);
  74.    
  75.    esc.write(PWMa);    
  76. }
  77.  
  78. void detectaimant()
  79.  {
  80.    etat= true;
  81.  }
  82.  
  83.  
  84.   /*
  85.    * https://liudr.wordpress.com/2012/01/16/sprintf/
  86.    * https://arduino.stackexchange.com/questions/19234/print-string-and-integer-lcd
  87.    * https://www.electric-skateboard.builders/t/recording-data-from-vesc-summary-of-tools/7020/13
  88.    */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement