Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. const int potPinOne = 14;
  2. const int potPinTwo = 15;
  3. const int ledPin = 13;
  4. const int buttonPin = 10;
  5.  
  6. int steerOne = 0;
  7. int steerTwo = 0;
  8.  
  9. String incomingByte;
  10.  
  11. int basis = 250;
  12. double distance1 = 0;
  13. double distance2 = 0;
  14. double tempDistance1 = 0;
  15. double tempDistance2 = 0;
  16. unsigned long timerPauzeCheck = 0;
  17. boolean paused1 = false;
  18. boolean paused2 = false;
  19. boolean gasActive1 = false;
  20. boolean gasActive2 = false;
  21. unsigned long timer1 = 0;
  22. unsigned long timer2 = 0;
  23.  
  24.  
  25. void setup() {
  26.   Serial.begin(9600);
  27.   pinMode(ledPin, OUTPUT);
  28.   pinMode(buttonPin, INPUT_PULLUP);
  29. }
  30.  
  31. void loop() {
  32.   steerOne = analogRead(potPinOne);
  33.   steerTwo = analogRead(potPinTwo);
  34.   Joystick.X(steerOne * 1.1);
  35.   Joystick.Y(steerTwo * 1.1);
  36.  
  37.  
  38.   incomingByte = Serial.readString();
  39.  
  40.   //Serial.println(incomingByte);
  41.  
  42.   int paceOne = getValue(incomingByte, ',', 0).toInt();
  43.   int paceTwo = getValue(incomingByte, ',', 1).toInt();
  44.  
  45.   gas(paceOne, basis, timer1, paused1, gasActive1, 1, 1);
  46.   gas(paceTwo, basis, timer2, paused2, gasActive2, 2, 2);
  47.  
  48. }
  49.  
  50. void gas(int pace, int basis, unsigned long timer, boolean paused, boolean gasActive, int knop, int ergometer) {
  51.   Serial.println("executing gas");
  52.   if (pace != 0 && millis() > timer) {
  53.     double pauze;
  54.     if (!gasActive && !paused) {
  55.       pauze = pow(((double)basis / (double)pace), 2) * 1000.0;
  56.       Joystick.button(knop, 1);
  57.       gasActive = true;
  58.     }
  59.     else {
  60.       pauze = pow(((double)pace / (double)basis), 2) * 1000.0;
  61.       Joystick.button(knop, 0);
  62.       gasActive = false;
  63.     }
  64.     timer = millis() + pauze;
  65.     if (ergometer == 1) {
  66.       gasActive1 = gasActive;
  67.       timer1 = timer;
  68.     }
  69.     else if (ergometer == 2) {
  70.       gasActive2 = gasActive;
  71.       timer2 = timer;
  72.     }
  73.   }
  74. }
  75.  
  76. String getValue(String data, char separator, int index)
  77. {
  78.   int found = 0;
  79.   int strIndex[] = {0, -1};
  80.   int maxIndex = data.length() - 1;
  81.  
  82.   for (int i = 0; i <= maxIndex && found <= index; i++) {
  83.     if (data.charAt(i) == separator || i == maxIndex) {
  84.       found++;
  85.       strIndex[0] = strIndex[1] + 1;
  86.       strIndex[1] = (i == maxIndex) ? i + 1 : i;
  87.     }
  88.   }
  89.  
  90.   return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement