// https://wokwi.com/projects/290883003139228169
// https://wokwi.com/makers/urish
#include <SoftwareSerial.h>
#define SEN_PIN A2 // Sensor
#define LED_PIN PB1
#define SERIAL_TX PB3
#define SERIAL_RX PB0
#define LED_DELAY 1000
bool switchLED = false;
unsigned long timerLED = 0;
int sensorValue = -1;
SoftwareSerial mySerial(SERIAL_RX, SERIAL_TX);
void setup() {
mySerial.begin(9600);
pinMode(SEN_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
analogWrite(SEN_PIN, 1);
mySerial.println("Start");
}
/*
void loop() {
sensorValue = analogRead(SEN_PIN);
if (millis() - timerLED >= LED_DELAY) {
timerLED = millis();
mySerial.print("Sensor Value: ");
mySerial.print(sensorValue);
mySerial.println();
switchLED = !switchLED;
digitalWrite(LED_PIN, switchLED ? HIGH : LOW);
}
}
*/
void loop() {
if (mySerial.available() > 0) {
String command = mySerial.readString(); // read the incoming command
if (command.startsWith("READ")) {
int analogPort = command.substring(5).toInt(); // extract the analog port number from the command
int value = analogRead(analogPort); // read the value from the analog port
String json = "{\"value\":" + String(value) + "}"; // format the value as a json string
mySerial.println(json); // print the json string
}
}
}