Skip to content

Commit 03b9183

Browse files
committed
Merge remote-tracking branch 'origin/oled_annimation'
2 parents 879e3b6 + df306b7 commit 03b9183

17 files changed

Lines changed: 1023 additions & 1 deletion

LL/SPI_master/SPI_master.ino

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <SPI.h>
2+
3+
const int SS_PIN = 10; // Slave Select pin
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
pinMode(SS_PIN, OUTPUT);
8+
digitalWrite(SS_PIN, HIGH); // Ensure SS stays high for now
9+
10+
SPI.begin(); // Initialize SPI
11+
SPI.setClockDivider(SPI_CLOCK_DIV128); // Set SPI clock to 1/8th of the system clock
12+
SPI.setDataMode(SPI_MODE0); // Set SPI mode to 0
13+
SPI.setBitOrder(MSBFIRST); // Set bit order to MSB first
14+
}
15+
16+
void loop() {
17+
digitalWrite(SS_PIN, LOW); // Enable slave
18+
delay(10); // Small delay to ensure slave is ready
19+
20+
float voltage_LiPo = 3.0;
21+
float voltage_NiMh = 5.0;
22+
23+
// Request data from the slave
24+
SPI.transfer(0x01); // Send a dummy byte to initiate the transfer
25+
SPI.transfer((byte*)&voltage_LiPo, sizeof(voltage_LiPo));
26+
SPI.transfer((byte*)&voltage_NiMh, sizeof(voltage_NiMh));
27+
28+
digitalWrite(SS_PIN, HIGH); // Disable slave
29+
30+
// Print the received voltages
31+
Serial.print("Voltage LiPo: ");
32+
Serial.print(voltage_LiPo);
33+
Serial.print("V, Voltage NiMh: ");
34+
Serial.println(voltage_NiMh);
35+
36+
delay(1); // Wait for a second before the next request
37+
}

LL/SPI_slave/SPI_slave.ino

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <SPI.h>
2+
3+
volatile bool dataReceived = false;
4+
volatile byte receivedData[8]; // Buffer to hold received data
5+
volatile byte index = 0;
6+
7+
void setup() {
8+
// Initialize Serial Monitor
9+
Serial.begin(9600);
10+
11+
// Set MISO as output
12+
pinMode(MISO, OUTPUT);
13+
14+
// Enable SPI as slave
15+
SPCR |= _BV(SPE);
16+
17+
// Attach interrupt to SPI
18+
SPI.attachInterrupt();
19+
}
20+
21+
ISR(SPI_STC_vect) {
22+
receivedData[index++] = SPDR; // Read received byte and store in buffer
23+
if (index >= 8) { // Check if we have received 8 bytes (2 floats)
24+
dataReceived = true;
25+
index = 0;
26+
}
27+
}
28+
29+
void loop() {
30+
if (dataReceived) {
31+
dataReceived = false;
32+
33+
// Convert received bytes to floats
34+
float received1, received2;
35+
memcpy(&received1, receivedData, sizeof(received1));
36+
memcpy(&received2, receivedData + sizeof(received1), sizeof(received2));
37+
38+
// Display received data on Serial Monitor
39+
Serial.print("Received float 1: ");
40+
Serial.println(received1);
41+
Serial.print("Received float 2: ");
42+
Serial.println(received2);
43+
}
44+
}

LL/ToF_I2C/ToF_I2C.ino

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "Adafruit_VL53L0X.h"
2+
3+
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
8+
// wait until serial port opens for native USB devices
9+
while (! Serial) {
10+
delay(1);
11+
}
12+
13+
Serial.println("Adafruit VL53L0X test.");
14+
if (!lox.begin()) {
15+
Serial.println(F("Failed to boot VL53L0X"));
16+
while(1);
17+
}
18+
// power
19+
Serial.println(F("VL53L0X API Continuous Ranging example\n\n"));
20+
21+
// start continuous ranging
22+
lox.startRangeContinuous();
23+
}
24+
25+
void loop() {
26+
if (lox.isRangeComplete()) {
27+
Serial.print("Distance in mm: ");
28+
Serial.println(lox.readRange());
29+
}
30+
}

LL/slaveI2C/slaveI2C.ino

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <Wire.h>
2+
3+
volatile bool dataReceived = false;
4+
volatile char receivedData[32]; // Buffer to hold received data
5+
volatile int receivedLength = 0;
6+
7+
void setup() {
8+
Wire.begin(8); // Join I2C bus with address #8
9+
Wire.onReceive(receiveEvent); // Register receive event
10+
Wire.onRequest(requestEvent); // Register request event
11+
Serial.begin(9600); // Start serial communication at 9600 bps
12+
}
13+
14+
void loop() {
15+
if (dataReceived) {
16+
dataReceived = false;
17+
Serial.print("Received: ");
18+
for (int i = 0; i < receivedLength; i++) {
19+
Serial.print(receivedData[i]);
20+
}
21+
Serial.println();
22+
}
23+
delay(100);
24+
}
25+
26+
// Function that executes whenever data is received from master
27+
void receiveEvent(int howMany) {
28+
receivedLength = 0;
29+
while (Wire.available()) {
30+
receivedData[receivedLength++] = Wire.read(); // Receive byte as a character
31+
}
32+
dataReceived = true;
33+
}
34+
35+
// Function that executes whenever data is requested by master
36+
void requestEvent() {
37+
const char* response = "Hello Master!";
38+
Wire.write(response);
39+
}

0 commit comments

Comments
 (0)