Need help merging these 2 codes together

This is the code I'm using for my project:


/**
 *  Draws a scrolling graph of a single analog input (set to A0) on the OLED display
 *
 *  By Jon E. Froehlich
 *  @jonfroehlich
 *  http://makeabilitylab.io
 *
 */

#include <Wire.h>
#include <SPI.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#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 = A0;
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;

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!_display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // Clear the buffer
  _display.clearDisplay();

  _display.setTextSize(1);
  _display.setTextColor(WHITE, BLACK);
  _display.setCursor(0, 0);
  _display.println("Screen initialized!");
  _display.display();
  delay(500);
  _display.clearDisplay();

  if(_drawStatusBar){
    _graphHeight = SCREEN_HEIGHT - 10;
  }

  _fpsStartTimeStamp = millis();

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(2, HIGH);
  delay(500);
  digitalWrite(3, HIGH);
  delay(500);
  digitalWrite(4, HIGH);
  delay(500);

}

void loop() {
  // 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);
  Serial.println(analogVal);
  _circularBuffer[_curWriteIndex++] = analogVal;

  // Set the circular buffer index back to zero when it reaches the 
  // right of the screen
  if(_curWriteIndex >= _display.width()){
    _curWriteIndex = 0;
  }
  
  if(_drawStatusBar){
    drawStatusBar(analogVal);
  }
  
  // Draw the line graph based on data in _circularBuffer
  int xPos = 0; 
  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++;;
  }
  

  _display.display();
  
  calcFrameRate();
  
  delay(DELAY_LOOP_MS);




}

/**
 * Draw the line at the given x position and analog value
 */
void drawLine(int xPos, int analogVal){
  int lineHeight = map(analogVal, MIN_ANALOG_INPUT, MAX_ANALOG_INPUT, 0, _graphHeight) * 17;
  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");

}

I also want to combine it with this script:

void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite(2, HIGH);
  delay(500);
  digitalWrite(3, HIGH);
  delay(500);
  digitalWrite(4, HIGH);
  delay(500);
}

void loop() {
   if (analogRead(A0) > 60)
{
  digitalWrite(4, HIGH);
  delay(1000);
}
else
{
  digitalWrite(4, LOW);
}
  if (analogRead(A0) > 20)
{
  digitalWrite(2, HIGH);
  delay(1000);
}
else
{
  digitalWrite(2, LOW);
}
  if (analogRead(A0) > 45)
{
  digitalWrite(3, HIGH);
  delay(1000);
}
else
{
  digitalWrite(3 , LOW);
}

  
  Serial.println(analogRead(A0));
  delay(20);
}

I'm a bit of a noob to Arduino coding and I tried combining them by putting the void setup and loop code of the second script into the first one but it didn't seem to work. Can someone help me by pointing out how 1 script can achieve the desired functionality?

Thanks in advance!

rather vague error report!
post the code (using code tags </>) ?
what happened? what should have happened?

I would have thought adding the loop() code of the second program to the loop() of the first should have been a start

what microcontroller are you using?

Thanks for the prompt reply Horace! Sorry but there are no errors raised actually. Instead all the elements of the second code don't work and the LEDs on pin 2, 3, 4 don't turn on at all but the rest of the code works. Can you please provide a version of the code that merges it? I will test it and let you know the results ASAP.

Oh and the microcontroller is an Arduino UNO R3.

what does

Serial.println(analogRead(A0));

print?

upload the merged program

It prints the analog signal from A0 (just checked) but the LED doesn't seem to turn on

quick question: the led is wired to digital GND from short leg and digital pin 2 3 and 4 on long leg. Could that be a potential reason?

Long leg are +ve so it is good. Did you add any resistor by the way

1 Like

no (it works without resistor, tested)

Are you sure . Can you test led only by powering single one

tested all 3 without resistor and they work
BTW can I wire resistors to any end or just +ve end?

Any end would be fine .Just need to control the flow current through the led

1 Like

If the led are not broken then can you share the merged code

I unfortunately no longer have it, got deleted when my pc restarted and I didn't save it! luckily saved the other 2

So what do you need

asking how to merge code so it works! When I did so the leds didnt work

ok BTW which are the pin's for display

A4 and A5

I didn't see that pin declared in code is it pre declared in the library

yes (wire.h)

Hi @gvbstudios ,

Welcome to the forum..

um, combining these 2 sketches will adversely affect the result..
the primary sketch is timing critical, you can't use any delay()'s in your added code..
don't really see why their there, so take em out..

good luck.. ~q