Advertisement
LLothar

Strobe detector

Mar 15th, 2015
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. //DISPLAY INI start
  2. #include <SPI.h>
  3. #include <Wire.h>
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_SSD1306.h>
  6.  
  7. #define OLED_RESET 4
  8. Adafruit_SSD1306 display(OLED_RESET);
  9.  
  10. #define NUMFLAKES 10
  11. #define XPOS 0
  12. #define YPOS 1
  13. #define DELTAY 2
  14.  
  15.  
  16. #if (SSD1306_LCDHEIGHT != 64)
  17. #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  18. #endif
  19.  
  20. //DISPLAY INI END
  21.  
  22.  
  23.  
  24. byte photoled = 1; //photoresistor or photodiode
  25.  
  26. int chart[128]; //128 reads will be displayed on an OLED which is 128px wide
  27.  
  28. byte x = 0; //counts 0 to 127 to fill the screen
  29. int maxVal; //max value, for chart scaling
  30. int minVal; //like above but min
  31. long oldmillis; //for frequency calculation
  32. float hejtow_na_floaty = 1; //shoutout to wykop.pl comment, which did not like my use of floats ;)
  33.  
  34. void setup() {
  35.  
  36.   Serial.begin(9600);
  37.  
  38.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  39.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  40.   // init done
  41.  
  42.   // Show image buffer on the display hardware.
  43.   // Since the buffer is intialized with an Adafruit splashscreen
  44.   // internally, this will display the splashscreen.
  45.   display.clearDisplay();// Clear the buffer.
  46.   display.setTextSize(1); //setup for the text
  47.   display.setTextColor(WHITE);
  48.  
  49. }
  50.  
  51. // the loop routine runs over and over again forever:
  52. void loop() {  
  53.  
  54.  
  55. int light = analogRead(photoled); //read the photoresistor, simple voltage divider with 1 megaohm resistor. I wanted to use an LED as photodiode, but photoresistor gave much better signal and was fast enough
  56.  
  57.  
  58.  
  59.  
  60. if (light > maxVal){ //hunt for the max value and min value
  61.   maxVal = light;
  62. }
  63.  
  64. if (light < minVal){
  65.   minVal = light;
  66. }
  67.  
  68. chart[x] = light; //put current read to an array
  69.  
  70. x++;
  71.  
  72.  
  73.  
  74. if (x == 128){ //time to display the chart!
  75.   display.setCursor(0,0);  // reset cursor
  76.   float Hz; //calculations for frequency of the screen
  77.   Hz = 1000/(millis() - oldmillis);
  78.   display.print(Hz);
  79.   display.print("Hz");
  80.   /*display.print("Max:");
  81.   display.println(maxVal);
  82.   display.print("Min:");
  83.   display.println(minVal);*/
  84.   float gap = maxVal - minVal; //this is for scaling, finding the gap
  85.   float scale = gap / 64;
  86.   float tempchart;
  87.   for (int i = 0; i < 128; i++){
  88.     chart[i] = chart[i] - minVal; //zeroing to min value
  89.     tempchart = chart[i] / scale;//and scaling
  90.     chart[i] = tempchart;
  91.    
  92.     if (i == 0){}
  93.     else{
  94.    
  95.     display.drawLine(i, 63-chart[i], i-1, 63 - chart[i-1],1); //drawing line. I just ignore the exception for i = 0, works fine still.
  96.     }
  97.     //display.drawPixel(i, 63-chart[i], 1);
  98.   }
  99.  
  100.  
  101.  display.display();
  102.  x = 0; //tidying up for the next go
  103.  maxVal = 0;
  104.  minVal = 1023;
  105.  display.clearDisplay();
  106.  delay(500); //wait half a second, let me lookm at the result
  107.  oldmillis = millis();
  108. }
  109.  
  110.  
  111.  
  112. //delayMicroseconds(1); //may be used to slow down the polling frequency
  113.  
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement