From 8109013e0b7bfa2d10d620a1cb26aece861cec14 Mon Sep 17 00:00:00 2001 From: Orell-Pieter Schwarzbach <2122623@stud.hs-mannheim.de> Date: Tue, 16 May 2023 11:01:57 +0200 Subject: [PATCH] Upload files to '' Final code for the arduino with i2c connection. LSB to 1 Byte --- Arduino_i2c.ino | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Arduino_i2c.ino diff --git a/Arduino_i2c.ino b/Arduino_i2c.ino new file mode 100644 index 0000000..b766c34 --- /dev/null +++ b/Arduino_i2c.ino @@ -0,0 +1,50 @@ +#include // I2C-Headerdatei + +const int adcPin = A0; // ADC-Pin +//const int toggleDelay = 0.1; // Verzögerung zwischen dem Umschalten der Bits, in Mikrosekunden +const int bufferSize = 8 * sizeof(uint32_t); + +void setup() { + Wire.begin(8); // I2C-Verbindung initialisieren + analogReadResolution(12); + //Serial.begin(9600); + + //Wire.onReceive(receiveData); + //Wire.onRequest(sendData); + +} + +/*void sendData() { + Wire.write(0xFF); + Serial.write(0xFF); +} + +void receiveData(int count) { + Serial.write('r'); +}*/ + +void loop() { + uint32_t buffer[8] = {0}; + uint8_t bitCount = 0; + + for (int i = 0; i < 8; i++) { + uint32_t randomValue = 0; + + while (bitCount < 32) { + uint16_t adcValue = analogRead(adcPin); + uint32_t newBit = adcValue & 0x01; + randomValue = (randomValue << 1) | newBit; + bitCount++; + //delay(toggleDelay); + } + + buffer[i] = randomValue; + bitCount = 0; + } + + Wire.beginTransmission(8); // I2C-Übertragung an Geräteadresse 8 starten + for (int i = 0; i < 8; i++) { + Wire.write((byte*)&buffer[i], sizeof(buffer[i])); // Daten über I2C-Bus senden + } + Wire.endTransmission(); // I2C-Übertragung beenden */ +}