I have two arduino nano boards.
The first arduino receives a altutude and temperature from bmp280 via I2C. Then this arduino sends these values on the second board via SPI. I used (d7 pin for it).
Master code
Спойлер
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include <Wire.h>
Adafruit_BMP280 bmp;
void setup (void) {
Serial.begin(115200); //set baud rate to 115200 for usart
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
while (1) delay(10);
}
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. /
Adafruit_BMP280::SAMPLING_X2, / Temp. oversampling /
Adafruit_BMP280::SAMPLING_X16, / Pressure oversampling /
Adafruit_BMP280::FILTER_X16, / Filtering. /
Adafruit_BMP280::STANDBY_MS_500); / Standby time. */
digitalWrite(7, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8
}
void loop (void) {
char c;
String data = String(bmp.readTemperature()) + ";" + String(bmp.readAltitude(1013.25)) + "\r";
digitalWrite(7, LOW); // enable Slave Select
// send test string
char* dataStr = data.c_str();
for (const char * p = dataStr ; c = *p; p++) {
SPI.transfer (c);
Serial.print(c);
}
digitalWrite(7, HIGH); // disable Slave Select
delay(200);
}
The second arduino receives these data via SPI and map it on the OLED display (via I2C).
Спойлер
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <OneTouch.h>
char buff [50];
volatile byte indx;
volatile boolean process;
#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)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
OneTouch first_button(2);
OneTouch second_button(3);
int y_coordinate = 5;
int i = 0;
//String par_names[8] = {"1.Date:", "2.Time:", "3.Alt.:", "4.Speed:", "5.Accel.:", "6.Gyro.:", "7.GPS:", "8.Pres.:"};
void setup (void) {
Serial.begin (115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for(;;);
}
delay(200);
display.clearDisplay();
display.setTextSize(1.7);
display.setTextColor(WHITE);
delay(200);
pinMode(MISO, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect){
byte c = SPDR; // read byte from SPI Data Register
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == '\r') //check for the end of the word
process = true;
}
}
char k[50];
void loop (void) {
display.display();
if (process) {
process = false; //reset the process
//Serial.println (buff); //print the array on serial monitor
indx= 0; //reset button to zero
for (int i = 0; i < 50; i++){
k[i] = buff[i];
//Serial.print(k[i]);
}
//Serial.println();
}
String temporary = String(k);
String temper = "";
temper.concat(temporary.charAt(0)); temper.concat(temporary.charAt(1));
temper.concat(temporary.charAt(2)); temper.concat(temporary.charAt(3));
temper.concat(temporary.charAt(4)); temper.concat(" C");
Serial.println(temper);
String par_names[8] = {"1.Temp: " + temper, "2.Alt: ", "3.Day.:",
"4.Speed:", "5.Accel.:", "6.Gyro.:",
"7.GPS:", "8.Pres.:"};
display.clearDisplay();
display.setCursor(0,y_coordinate);
display.println(par_names[i]);
display.setCursor(0,y_coordinate + 20);
display.println(par_names[i + 1]);
display.setCursor(0,y_coordinate + 40);
display.println(par_names[i + 2]);
display.display();
if (first_button.get_state()){
if (i > 0 ){
i -= 1;
}
}
if (second_button.get_state()){
if (i < 5){
i += 1;
}
}
delay(50);
}
It does not display first row. Also I the buttons don't work. It looks like it hangs.