erikzandboer

Model helicopter simulation

Aug 25th, 2020 (edited)
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.73 KB | None | 0 0
  1. /*--------------------------------------------------------------------------------------------*/
  2. /* Helicopter led & motor simulation.                                                         */
  3. /*                                                                                            */
  4. /* Controls two leds and a motor to simulate starting up of a model helicopter                */
  5. /*                                                                                            */
  6. /* Versioning:                                                                                */
  7. /* 1.00            Creation. First go at timing the leds and motor. No audio included yet     */
  8. /* 1.10            Added a second blinking group and a second motor control (main and rear)   */
  9. /* 1.11            Bugfix around BlinkTimer: Checked for zero were lowest indent is 1.Fixed.  */
  10. /*--------------------------------------------------------------------------------------------*/
  11.  
  12. // Includes
  13. #include <Arduino.h>
  14.  
  15. // Define the physical pinout
  16. #define BLINKLED  8
  17. #define BLINKLED2 7
  18. #define MOTOR     5
  19. #define RMOTOR    6
  20. #define SOLIDLED  13
  21.  
  22. // Get some base counters in for the runtime in seconds.
  23. byte            Hundreds=0;
  24. unsigned int    RunTime=0;
  25.  
  26. // Global variables
  27. byte           MotorSpeed = 0;  // Speed at which the motor turns. Used in the Motor state machine.
  28. unsigned int   BlinkTimer = 0;  // Timer to control the blinking led.
  29.  
  30. // State machine for the motor
  31. #define MOTOROFF  0
  32. #define MOTORREVV 1
  33. #define MOTORRUN  2
  34. byte    MotorState = MOTOROFF;
  35.  
  36. // This runs only once when powering on
  37. void setup()
  38. {
  39.         digitalWrite(BLINKLED,LOW);  // Blinking led group starts OFF
  40.         digitalWrite(BLINKLED2,LOW); // Blinking led group 2 starts OFF
  41.         digitalWrite(MOTOR,LOW);     // Motor is OFF
  42.         digitalWrite(RMOTOR,LOW);    // Rear Motor is OFF
  43.         digitalWrite(SOLIDLED,HIGH); // Solid leds are always ON
  44.  
  45.         pinMode (BLINKLED2, OUTPUT);
  46.         pinMode (BLINKLED,  OUTPUT);
  47.         pinMode (MOTOR,     OUTPUT);
  48.         pinMode (RMOTOR,    OUTPUT);
  49.         pinMode (SOLIDLED,  OUTPUT);
  50. }
  51.  
  52. // This loops forever.
  53. void loop()
  54. {
  55.         while ( (millis() % 10) != 0L ) //Just do nothing until millis()/10 has no remainder --> Run at 100Hz
  56.         {
  57.                 asm("nop \n"); // Just do NOP to make sure the compiler doesn't optimize the while() away
  58.         }
  59.  
  60.         // This code executes 100 times a second
  61.         Hundreds++;
  62.         if (Hundreds > 99)
  63.         {
  64.                 Hundreds = 0;   // Counts up to a single second, then increase the RunTime
  65.                 RunTime++;
  66.         }
  67.  
  68.         // Here we check if any led should still be running; Stop a few seconds after the engine stops
  69.         if (RunTime < 73)
  70.         {
  71.                 // HANDLE BLINKTIMER. Currently on a 0.5s frequency and lights both groups for 0.1s, 0.2s apart.
  72.                 BlinkTimer++;
  73.                 if (BlinkTimer == 1 ) digitalWrite(BLINKLED,HIGH);
  74.                 if (BlinkTimer == 11) digitalWrite(BLINKLED,LOW);
  75.                 if (BlinkTimer == 21) digitalWrite(BLINKLED2,HIGH);
  76.                 if (BlinkTimer == 31) digitalWrite(BLINKLED2,LOW);
  77.                 if (BlinkTimer >  49)
  78.                 {
  79.                         BlinkTimer = 0;
  80.                 }
  81.         }
  82.         else    // After 73 seconds kill the blinkleds and kill the solid leds.
  83.         {
  84.                 digitalWrite(BLINKLED,LOW);
  85.                 digitalWrite(BLINKLED2,LOW);
  86.                 digitalWrite(SOLIDLED, LOW);
  87.         }
  88.  
  89.         // HANDLE THE MOTOR state machine
  90.         switch (MotorState)
  91.         {
  92.                 case MOTOROFF:
  93.                         if (RunTime == 30)      // Start revving after 30 seconds
  94.                         {
  95.                                 analogWrite(MOTOR,255); // Kick the motor at full speed to make sure it starts turning
  96.                                 analogWrite(RMOTOR,255);        // Kick the motor at full speed to make sure it starts turning
  97.                                 MotorSpeed = 10;        // ... MotorRevv will slow down the motor again after 1/20th of a second.
  98.                                 MotorState = MOTORREVV;
  99.                         }
  100.                         break;
  101.                 case MOTORREVV:
  102.                         if ((Hundreds % 5) == 0) // 1/20th of seconds so revving up will take 12.25 seconds
  103.                         {
  104.                                 MotorSpeed++;
  105.                                 if (MotorSpeed == 255)
  106.                                 {
  107.                                         MotorState = MOTORRUN;
  108.                                 }
  109.                                 analogWrite(MOTOR,MotorSpeed);
  110.                                 analogWrite(RMOTOR,MotorSpeed);
  111.                         }
  112.                         break;
  113.                 case MOTORRUN:
  114.                         if (RunTime == 70)
  115.                         {
  116.                                 MotorState = MOTOROFF;
  117.                                 analogWrite(MOTOR,0);
  118.                                 analogWrite(RMOTOR,0);
  119.                         }
  120.                         break;
  121.                 default: // This should never occur as it represents an illegal state. If it occurs, reset the state machine.
  122.                         MotorState = MOTOROFF;
  123.         }
  124.  
  125.         // Reset after 15 minutes and start over
  126.         if (RunTime == 15*60)
  127.         {
  128.                 RunTime = 0;
  129.                 MotorState = MOTOROFF;
  130.                 digitalWrite(SOLIDLED,HIGH); // Restart so we start the solid leds again; blink led should start itself
  131.         }
  132.  
  133.         delay(1); // Added this dummy delay() to make sure the code takes more than 1 ms to execute.
  134. }
  135.  
Add Comment
Please, Sign In to add comment