0

I know this question has been asked before but I am having trouble running I2c and SPI at the same time. I am attaching my code below. The problem when I run the code is that the LED's don't blink until the OLED function has been complete, can I fix this with hardware? do I need to fix this in the code? (please note I am using a teensy 3.2)

My end goal is to be able to initially turn on the teensy, choose a pattern on the OLED for what led's turn on, and then just have the leds run.

code :

#include <SPI.h>
SPISettings settingsA(2000000, MSBFIRST, SPI_MODE1);
#include <SFE_MicroOLED.h>  // Include the SFE_MicroOLED library
#include <Wire.h>  // Include Wire if you're using I2C


int time_interval_for_chip = 1;
int blinking_interval = 500;
unsigned long current_time = 0;
#define PIN_RESET 9  // Connect RST to pin 9

#define DC_JUMPER 0

MicroOLED oled(PIN_RESET, DC_JUMPER);    // I2C declaration

void setup() {
  // put your setup code here, to run once:
  pinMode(10, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
  Serial.begin(9600);
  SPI.begin();
  SPI.beginTransaction(settingsA);

  oled.begin();    // Initialize the OLED
  oled.clear(ALL); // Clear the display's internal memory
  oled.display();  // Display what's in the buffer (splashscreen)
  delay(1000);     // Delay 1000 ms
  oled.clear(PAGE); // Clear the buffer.

  randomSeed(analogRead(A0) + analogRead(A1));

  //SPISettings object is used to configure the SPI port for your SPI device. All 3 parameters are combined to a single SPISettings object, which is given to SPI.beginTransaction().
  //sending in bits by MSB first and using SPI_MODE1 which is output edge rising data capture falling
  // can be set to either 4000000 or 2000000
}

void loop() {

  //Blinks output 4 and 8 on and off with teensy without spi.endtrasaction()
  //delaying with delay void function made
  unsigned int result = 0;
  unsigned int result2 = 0;
  byte inbyte = 0;
  byte inbyte2 = 0;

  digitalWrite(10, LOW);
  result2 = SPI.transfer16(0b0100000100000100);
  Serial.print("result= ");
  Serial.println(result2, BIN);
  digitalWrite(10, HIGH);

  //getting the time
  current_time = millis();
  delaying(current_time, 1);



  digitalWrite(10, LOW);
  result = SPI.transfer16(0b0000010000010000);
  Serial.print("result2= ");
  Serial.println(result, BIN);
  digitalWrite(10, HIGH);


  //getting the time
  current_time = millis();
  delaying(current_time, 300);


  digitalWrite(10, LOW);
  result2 = SPI.transfer16(0b0100000000000100);
  Serial.print("result1= ");
  Serial.println(result2, BIN);
  digitalWrite(10, HIGH);

  //getting the time
  current_time = millis();
  delaying(current_time, 1);


  digitalWrite(10, LOW);
  result = SPI.transfer16(0b0000000000010000);
  Serial.print("result2= ");
  Serial.println(result, BIN);
  digitalWrite(10, HIGH);


  //getting the time
  current_time = millis();
  delaying(current_time, 100);
  lineExample();  // Run the pixel example function

}






void lineExample()
{
  int middleX = oled.getLCDWidth() / 2;
  int middleY = oled.getLCDHeight() / 2;
  int xEnd, yEnd;
  int lineWidth = min(middleX, middleY);

  printTitle("Lines!", 1);

  for (int i=0; i<3; i++)
  {
    for (int deg=0; deg<360; deg+=15)
    {
      xEnd = lineWidth * cos(deg * PI / 180.0);
      yEnd = lineWidth * sin(deg * PI / 180.0);

      oled.line(middleX, middleY, middleX + xEnd, middleY + yEnd);
      oled.display();
      delay(10);
    }
    for (int deg=0; deg<360; deg+=15)
    {
      xEnd = lineWidth * cos(deg * PI / 180.0);
      yEnd = lineWidth * sin(deg * PI / 180.0);

      oled.line(middleX, middleY, middleX + xEnd, middleY + yEnd, BLACK, NORM);
      oled.display();
       current_time = millis();
  delaying(current_time, 10);
    }
  }
}

void printTitle(String title, int font)
{
  int middleX = oled.getLCDWidth() / 2;
  int middleY = oled.getLCDHeight() / 2;

  oled.clear(PAGE);
  oled.setFontType(font);
  // Try to set the cursor in the middle of the screen
  oled.setCursor(middleX - (oled.getFontWidth() * (title.length() / 2)),
                 middleY - (oled.getFontWidth() / 2));
  // Print the title:
  oled.print(title);
  oled.display();

  //getting the time
  current_time = millis();
  delaying(current_time, 1500);
  oled.clear(PAGE);
}

void delaying(unsigned long current_time, int period) {

  while (millis() < current_time + period) {
    //wait approx. [period] ms
  }
}
  • 1
    Can you replace the use of millis with normal delay and test again? The usage of millis is wrong, your sketch could delay by 50 days sometimes. You say the the blinking on the leds is delayed. How much slower is it? The i2c bus is slow, and all those delays and millis make the sketch even slower. Why do you have so many delays and complain about the delays? I count 1.4 seconds delay in the function lineExample alone. – Jot Mar 22 '18 at 21:32
  • can you explain how I am using millis() wrong? that would be constructive – Brit Mar 22 '18 at 21:37
  • See the blink without delay example: arduino.cc/en/Tutorial/BlinkWithoutDelay As soon as you compare a value to the return value of millis(), that's wrong. You can use the blink without delay twice to do multiple things at once. Update the SPI at a fast rate and the oled at a slow rate. – Jot Mar 22 '18 at 21:40
  • ok I see what you mean for 1 but I don't see how this is wrong. I followed this link norwegiancreations.com/2017/09/… – Brit Mar 22 '18 at 21:43
  • what my code is doing is it won't run the OLED and the led's at the same time, the OLED function needs to be complete before the leds run but I want to run the two at the same time. – Brit Mar 22 '18 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.