Created
June 29, 2020 23:23
-
-
Save mikegreen/5f641c535a16924466a2b36562525c00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#define SLAVE_ADDRESS 0x04 | |
#define LED 6 | |
int number = 0; | |
// Pin that input voltage and resistors are connected | |
// doc here resistor setup | |
int inputPin = A0 ; | |
// calculation to get voltage from analog reading | |
double voltageMultiplier = 1023/5/3.10 ; | |
int val; | |
double volts; | |
String voltsPrefix = "Volts: "; | |
String piOutput; | |
void setup() { | |
Serial.println("Starting setup..."); | |
Serial.begin(9600); | |
// i2c setup | |
Wire.begin(SLAVE_ADDRESS); | |
Wire.onReceive(receiveData); | |
Wire.onRequest(sendData); | |
Serial.println("Ready!"); | |
} | |
void loop() | |
{ | |
val = analogRead(inputPin); | |
Serial.print("Analog is: "); | |
Serial.print(val); | |
Serial.print(", volts: "); | |
// get volts based on the multiplier from resistor voltage divider | |
volts = val / voltageMultiplier; | |
Serial.println(volts); | |
piOutput = ("Volts:" + String(volts) + "|RawAnalog:" + val + "|"); | |
// char buff[10]; | |
// sprintf(buff, "00%d", int(volts*100)); | |
// Serial.print(buff); | |
Serial.println(piOutput); | |
delay(500); | |
voltsPrefix = ""; | |
} | |
void receiveData(int byteCount) { | |
Serial.println("receiveData"); | |
while (Wire.available()) { | |
number = Wire.read(); | |
Serial.print("data received: "); | |
Serial.println(number); | |
if (number == 1) { | |
Serial.println(" LED ON"); | |
digitalWrite(LED, HIGH); | |
} else { | |
Serial.println(" LED OFF"); | |
digitalWrite(LED, LOW); | |
} | |
} | |
} | |
void sendData() { | |
// Wire.write(number); | |
Serial.println("Sending to pi..." + piOutput); | |
Wire.write(piOutput.c_str()); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment