Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DISPLAY INI start
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- #define NUMFLAKES 10
- #define XPOS 0
- #define YPOS 1
- #define DELTAY 2
- #if (SSD1306_LCDHEIGHT != 64)
- #error("Height incorrect, please fix Adafruit_SSD1306.h!");
- #endif
- //DISPLAY INI END
- byte photoled = 1; //photoresistor or photodiode
- int chart[128]; //128 reads will be displayed on an OLED which is 128px wide
- byte x = 0; //counts 0 to 127 to fill the screen
- int maxVal; //max value, for chart scaling
- int minVal; //like above but min
- long oldmillis; //for frequency calculation
- float hejtow_na_floaty = 1; //shoutout to wykop.pl comment, which did not like my use of floats ;)
- void setup() {
- Serial.begin(9600);
- // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
- // init done
- // Show image buffer on the display hardware.
- // Since the buffer is intialized with an Adafruit splashscreen
- // internally, this will display the splashscreen.
- display.clearDisplay();// Clear the buffer.
- display.setTextSize(1); //setup for the text
- display.setTextColor(WHITE);
- }
- // the loop routine runs over and over again forever:
- void loop() {
- 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
- if (light > maxVal){ //hunt for the max value and min value
- maxVal = light;
- }
- if (light < minVal){
- minVal = light;
- }
- chart[x] = light; //put current read to an array
- x++;
- if (x == 128){ //time to display the chart!
- display.setCursor(0,0); // reset cursor
- float Hz; //calculations for frequency of the screen
- Hz = 1000/(millis() - oldmillis);
- display.print(Hz);
- display.print("Hz");
- /*display.print("Max:");
- display.println(maxVal);
- display.print("Min:");
- display.println(minVal);*/
- float gap = maxVal - minVal; //this is for scaling, finding the gap
- float scale = gap / 64;
- float tempchart;
- for (int i = 0; i < 128; i++){
- chart[i] = chart[i] - minVal; //zeroing to min value
- tempchart = chart[i] / scale;//and scaling
- chart[i] = tempchart;
- if (i == 0){}
- else{
- 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.
- }
- //display.drawPixel(i, 63-chart[i], 1);
- }
- display.display();
- x = 0; //tidying up for the next go
- maxVal = 0;
- minVal = 1023;
- display.clearDisplay();
- delay(500); //wait half a second, let me lookm at the result
- oldmillis = millis();
- }
- //delayMicroseconds(1); //may be used to slow down the polling frequency
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement