Advertisement
Guest User

Corrected Arduino Strobe

a guest
Oct 25th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. /*
  2.  * Shitty strobe light
  3.  * kerai 2014
  4. */
  5.  
  6. // duhh I'm dumb, changed to use reciprocal of the earlier BPM division thing, whatever
  7.  
  8. //////
  9. ////// ARDUINO PART
  10. //////
  11.  
  12. // the setup function runs once when you press reset or power the board
  13. void setup() {
  14.   // initialize digital pin 13 as an output.
  15.   pinMode(12, OUTPUT);
  16.   Serial.begin(115200);
  17. }
  18.  
  19. typedef unsigned long MicroCounter;
  20.  
  21. MicroCounter delay_sync_time=0;
  22. MicroCounter delay_period=100;
  23.  
  24. #define MICROSECONDS_IN_ONE_SECOND_F 1000000.f
  25.  
  26. // the loop function runs over and over again forever
  27. void loop() {
  28.   if(Serial.available()){
  29.     byte bpm = Serial.read();
  30.     Serial.print(bpm, DEC);
  31.     Serial.println("bpm");
  32.     float bpmf = (float) bpm;
  33.    delay_period = (unsigned) MICROSECONDS_IN_ONE_SECOND_F * (60.f / bpmf);
  34.  
  35.     delay_sync_time = micros();
  36.   }
  37.   MicroCounter time = micros();
  38.   MicroCounter delta = time - delay_sync_time;
  39.   if(delta > delay_period){
  40.     delay_sync_time += delta;
  41.     digitalWrite(12,!digitalRead(12));
  42.   }
  43. }
  44.  
  45. //////
  46. ////// UNIX PART
  47. //////
  48.  
  49. #include <stdio.h>
  50. #include <stdint.h>
  51.  
  52. int main(void){
  53.     fprintf(stderr, "Enter number: ");
  54.     unsigned bpm=0;
  55.     scanf("%u", &bpm);
  56.     uint8_t byte_bpm = (uint8_t) bpm;
  57.     fprintf(stderr, "Got %u, byte value %hhu\n", bpm, byte_bpm);
  58.     fwrite(&byte_bpm,1,1,stdout);
  59. }
  60.  
  61. //////
  62. ////// How to use
  63. //////
  64. /*
  65. Put code on arduino.
  66.  
  67. Open up a shell, compile dec_number_to_bin.c
  68.  
  69. ./dec_number_bin > /dev/ttyACM0
  70.  
  71. Follow prompt, etc.
  72.  
  73. Written drunk, it works somehow, you're on your own synchronising it to audio.*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement