Yo thank you my man. I follow ur suggestion, it can compile but both code doesnt function, oled screen still blank, and start button doesnt trigger led bpm. And i also put second pot for graph scrolling, so its input from A2
Is this right?
#include <uClock.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define ANALOG_SYNC_RATIO 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 _display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int ANALOG_INPUT_PIN = A2;
const int MIN_ANALOG_INPUT = 0;
const int MAX_ANALOG_INPUT = 1023;
const int DELAY_LOOP_MS = 5; // change to slow down how often to read and graph value
int _circularBuffer[SCREEN_WIDTH]; //fast way to store values
int _curWriteIndex = 0; // tracks where we are in the circular buffer
// for tracking fps
float _fps = 0;
unsigned long _frameCount = 0;
unsigned long _fpsStartTimeStamp = 0;
// status bar
boolean _drawStatusBar = true; // change to show/hide status bar
int _graphHeight = SCREEN_HEIGHT;
bool screen_overflow=false;
bool currentState = false;
bool currentSwitchState = false;
bool needsToSendMidiStart = false;
const byte pinCount = 4;
byte digitalPinOut[pinCount] = {3,5,7,9}; //Define analog clock outputs here
void clockOutput96PPQN(uint32_t* tick) {
if (needsToSendMidiStart) {
needsToSendMidiStart = false;
Serial.write(0xFA);
}
Serial.write(0xF8);
}
void clockOutput32PPQN(uint32_t* tick) {
if (currentState) {
if ((*tick % ANALOG_SYNC_RATIO ) == 0) {
sendDigitalOut(true);
} else {
sendDigitalOut(false);
}
}
}
void sendDigitalOut(bool state) {
byte pinState = state ? HIGH : LOW;
for (byte i = 0; i < pinCount; i++) {
digitalWrite(digitalPinOut[i], pinState);
}
}
void setup() {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
_display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// Address 0x3D for 128x64
_display.setTextSize(1);
_display.setTextColor(WHITE, BLACK);
_display.setCursor(0, 0);
_display.clearDisplay();
_graphHeight = SCREEN_HEIGHT - 10;
_fpsStartTimeStamp = millis();
Serial.begin(31250);
uClock.init();
pinMode(2, INPUT);
for (byte i = 0; i < pinCount; i++) {
pinMode(digitalPinOut[i], OUTPUT);
}
uClock.init();
uClock.setClock96PPQNOutput(clockOutput96PPQN);
uClock.setClock32PPQNOutput(clockOutput32PPQN);
uClock.setTempo(96);
uClock.start();
}
void toggleStartStop() {
if (currentState) {
Serial.write(0xFC);
sendDigitalOut(false);
currentState = false;
} else {
uClock.stop();
delay(20);
currentState = true;
needsToSendMidiStart = true;
uClock.start();
}
}
void loop() {
clockbox();
graph();
}
void graph() {
// Clear the display on each frame. We draw from the _circularBuffer
_display.clearDisplay();
// Read and store the analog data into a circular buffer
int analogVal = analogRead(ANALOG_INPUT_PIN);
_circularBuffer[_curWriteIndex++] = analogVal;
// Set the circular buffer index back to zero when it reaches the
// right of the screen
if(_curWriteIndex >= 128){
_curWriteIndex = 0;
}
drawStatusBar(analogVal);
// Draw the line graph based on data in _circularBuffer
int xPos = 0;
if(screen_overflow==true){
for (int i = _curWriteIndex; i < _display.width(); i++){
int analogVal = _circularBuffer[i];
drawLine(xPos, analogVal);
xPos++;
}
for(int i = 0; i < _curWriteIndex; i++){
int analogVal = _circularBuffer[i];
drawLine(xPos, analogVal);
xPos++;
}
}else if(screen_overflow==false){
for(int i = 0; i < _curWriteIndex; i++){
int analogVal = _circularBuffer[i];
drawLine(xPos, analogVal);
xPos++;
}
if(xPos==127)
{
screen_overflow=true;
}
}
_display.display();
calcFrameRate();
delay(DELAY_LOOP_MS);
}
void clockbox() {
int tempoPot = analogRead(A0);
float tempo = ((float)tempoPot/1024.f)*210.f + 30.f;
uClock.setTempo(tempo);
int switchState = digitalRead(2);
if ((switchState == HIGH) && !currentSwitchState) {
toggleStartStop();
currentSwitchState = true;
}
if (switchState == LOW) {
currentSwitchState = false;
}
delay(30);
}
void drawLine(int xPos, int analogVal){
int lineHeight = map(analogVal, MIN_ANALOG_INPUT, MAX_ANALOG_INPUT, 0, _graphHeight);
int yPos = _display.height() - lineHeight;
_display.drawFastVLine(xPos, yPos, lineHeight, SSD1306_WHITE);
}
/**
- Call this every frame to calculate frame rate
*/
void calcFrameRate() {
unsigned long elapsedTime = millis() - _fpsStartTimeStamp;
_frameCount++;
if (elapsedTime > 1000) {
_fps = _frameCount / (elapsedTime / 1000.0);
_fpsStartTimeStamp = millis();
_frameCount = 0;
}
}
/**
-
Draws the status bar at top of screen with fps and analog value
*/
void drawStatusBar(int analogVal) {
// erase status bar by drawing all black
_display.fillRect(0, 0, _display.width(), 8, SSD1306_BLACK);
// Draw current val
_display.setCursor(0, 0);
_display.print(analogVal);
// Draw frame count
int16_t x1, y1;
uint16_t w, h;
_display.getTextBounds(“XX.XX fps”, 0, 0, &x1, &y1, &w, &h);
_display.setCursor(_display.width() - w, 0);
_display.print(_fps);
_display.print(" fps");
}