Advertisement
Bukisoh

Vélo électrique v8

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