Advertisement
thepowderguy

Arduino Code Wave Power Experiment

May 30th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include "Servo.h"
  2. #include <math.h>
  3.  
  4. #define SQUARE 0
  5. #define SINE 1
  6. #define TRIANGLE 2
  7.  
  8. // Usage Guide
  9. //
  10. // Enter commands through serial. A command is formed by the identifier, followed by arguments, and finally a period.
  11. //
  12. // List of commands
  13. //
  14. // w [Min angle] [Max angle] [Wave type] [Period, multiples of 20ms]
  15. // Rotates the servo at a set interval
  16. // Example: "w 70 100 0 15."
  17. // Rotates servo between 70 and 100 degrees, using a square wave, and period is 15*20 = 300ms
  18. //
  19. // rc [period]
  20. // rv [period]
  21. // collects current or voltage data, respectively
  22. // Example: "rc 25."
  23. // Collects current data with intervals of 25*20 = 500ms
  24. //
  25.  
  26. // Pin definitions. You can redefine
  27. #define SERVO_PIN 5
  28. #define READ_PIN 1
  29. #define MUX_PIN 6
  30.  
  31. #define DATA_POINTS 100
  32.  
  33. Servo servo;
  34.  
  35. void setup() {
  36.   Serial.begin(9600);
  37.   pinMode(LED_BUILTIN, OUTPUT);
  38.   pinMode(MUX_PIN, OUTPUT);
  39.   digitalWrite(MUX_PIN, false);
  40.   servo.attach(SERVO_PIN);
  41. }
  42.  
  43. int period = 1000;
  44. int data[DATA_POINTS] = {0};
  45. float low = 45.0;
  46. float high = 135.0;
  47. int mode = SQUARE;
  48. int tick = 0;
  49. bool led_st = false;
  50. String input_mode;
  51. String input_rp;
  52. String input_h;
  53. String input_l;
  54. String input_t;
  55. String input_p;
  56. int index = -1;
  57. int rp = 10;
  58. void loop() {
  59.   if (Serial.available() > 0) {
  60.     input_mode = Serial.readStringUntil(' ');
  61.     if (input_mode == "w") {
  62.       input_h = Serial.readStringUntil(' ');
  63.       input_l = Serial.readStringUntil(' ');
  64.       input_t = Serial.readStringUntil(' ');
  65.       input_p = Serial.readStringUntil('.');
  66.       high = atof(input_h.c_str());
  67.       low = atof(input_l.c_str());
  68.       mode = atoi(input_t.c_str());
  69.       period = atoi(input_p.c_str());
  70.     } else if (input_mode == "rv") {
  71.       input_rp = Serial.readStringUntil('.');
  72.       rp = atoi(input_rp.c_str());
  73.       index = 0;
  74.     } else if (input_mode == "rc") {
  75.       input_rp = Serial.readStringUntil('.');
  76.       rp = atoi(input_rp.c_str());
  77.       index = 0;
  78.       digitalWrite(MUX_PIN, true);
  79.     }
  80.   }
  81.   digitalWrite(LED_BUILTIN, led_st);
  82.   if (tick%period == 0 || tick % period == period/2)
  83.     led_st = !led_st;
  84.   if (tick%rp == 0) {
  85.     if (index >= 0) {
  86.       data[index] = analogRead(READ_PIN);
  87.       index++;
  88.       if (index == DATA_POINTS) {
  89.         index = -1;
  90.         digitalWrite(MUX_PIN, false);
  91.         Serial.write("START: ");
  92.         for (int i = 0; i < DATA_POINTS; i++) {
  93.           Serial.print(data[i]);
  94.           Serial.write(", ");
  95.         }
  96.         Serial.write("END");
  97.       }
  98.     }
  99.   }
  100.   float angle = 90.0;
  101.   switch(mode) {
  102.     case SQUARE:
  103.       angle = ((tick%period < period/2)? low : high);
  104.       break;
  105.     case SINE:
  106.       angle = (high-low)*(sin(2.0 * PI * (float)tick / (float)period) + 1.0) / 2.0 + low;
  107.       break;
  108.     case TRIANGLE:
  109.       angle = (high-low)*(2.0*fabs((float)(tick%period) - period/2.0)) / period + low;
  110.       break;
  111.   }
  112.   servo.write(angle);
  113.   tick++;
  114.   delay(20);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement