Advertisement
ksoltan

pwm_deflection_test.ino

Mar 8th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. /**
  2. * Loop through duty cycles from 0 to 100%, letting the fin flap a couple of times at each setting.
  3. * The goal of this code is to determine the response of the hinge to different PWM values.
  4. * Future improvement: let fin flap at different frequencies for each setting.
  5. * Current improvement: Pwm is on for a certain percentage of the time. (Oooh, create test modes)
  6. *                       -> pretty easy, update duty, update flap like cuttlespeak
  7. */
  8.  
  9. // ARDUINO PINS FOR MICs
  10. int head_r = 3; // A2
  11. int head_l = 5; // A3
  12. int LED = 13;
  13.  
  14. int curr_duty = 50;
  15. int dir = 0;
  16. int num_flaps_per_duty = 4;
  17. int curr_flap = 1;
  18. int time_to_switch_dir = 2000; // Allow for 2s between each fin direction change
  19. int curr_time_percentage = 100;
  20.  
  21. void setup() {
  22.   pinMode(LED, OUTPUT);
  23.   Serial.begin(9600);
  24. }
  25.  
  26. void loop() {
  27.   if(curr_flap > num_flaps_per_duty){
  28. //    updateDutyCycle();
  29.     curr_flap = 1;
  30.   }
  31.   flap();
  32. }
  33.  
  34. void updateDutyCycle(){
  35.   if (curr_duty < 25) {
  36.     curr_duty = curr_duty + 2;
  37.   } else {
  38.     curr_duty = 1; // Start the loop over.
  39.     Serial.println("Starting new duty cycle");
  40.   }
  41.   Serial.println(curr_duty);
  42. }
  43.  
  44. void updateTimePercentage(){
  45.   if (curr_time_percentage < 40) {
  46.     curr_time_percentage = curr_time_percentage + 5;
  47.   } else {
  48.     curr_time_percentage = 1; // Start the loop over.
  49.     Serial.println("Starting new time percentage cycle");
  50.   }
  51.   Serial.println(curr_time_percentage);
  52. }
  53.  
  54. static long last_changed = 0;
  55. void flap(){
  56.   // Check if need to switch direction.
  57.   // Since this control sequence keeps pwm signal constant, need to set it once.
  58.   if(millis() - last_changed >= time_to_switch_dir){
  59.     dir = 1 - dir;
  60.     last_changed = millis();
  61.     curr_flap++;
  62.     Serial.println("Flap.");
  63.   }
  64.   move_fin();
  65. }
  66.  
  67. void move_fin(){
  68.   int pwm = (int)(255 * curr_duty / 100);
  69. //  Serial.print("Diff between last changed and current time:");
  70. //  Serial.print(millis() - last_changed);
  71. //  Serial.print(".\n");
  72. //  Serial.print("Time percentage range:");
  73. //  Serial.print(curr_time_percentage / 100.0 * time_to_switch_dir);
  74. //  Serial.print(".\n");
  75.   if(millis() - last_changed <= curr_time_percentage / 100.0 * time_to_switch_dir){ // Only supply pwm during time percentage
  76.     if (dir) {
  77.       Serial.println("RIGHT");
  78.       analogWrite(head_r, pwm); // Set pwm to move right.
  79.       analogWrite(head_l, 0);
  80.       digitalWrite(LED, HIGH);
  81.     }
  82.     else {
  83.       Serial.println("LEFT");
  84.       analogWrite(head_r, 0);
  85.       analogWrite(head_l, pwm); // Set pwm to move left.
  86.       digitalWrite(LED, LOW);
  87.     }
  88.   } else{
  89.       Serial.println("Zero PWM!!!");
  90.       analogWrite(head_r, 0);
  91.       analogWrite(head_l, 0); // Set pwm to move left.
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement