#include <Particle.h>
#include <Servo.h>
// Set up ADC pins for current and voltage readings
int currentPin = A0;
int voltagePin = A1;
// Set up ADC pin for throttle input
int throttlePin = A2;
// Set up button pin and servo
int buttonPin = D0;
Servo servo;
// Set servo positions
int position1 = 0; // Initial position
int position2 = 45; // Middle position
int position3 = 90; // End position
// Set up webhook to publish data to Adafruit IO
void publishData(double voltage, double current, double throttle) {
String data = "{";
data += "\"voltage\": " + String(voltage, 2) + ",";
data += "\"current\": " + String(current, 2) + ",";
data += "\"throttle\": " + String(throttle, 2) + "}";
Particle.publish("car-data", data, PRIVATE);
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set up ADC pins
pinMode(currentPin, INPUT);
pinMode(voltagePin, INPUT);
pinMode(throttlePin, INPUT);
// Set up button pin and attach interrupt
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(buttonPin, buttonISR, FALLING);
// Set up servo
servo.attach(D1);
servo.write(position1);
}
void loop() {
// Read voltage and current
double voltage = analogRead(voltagePin) * 0.00488;
double current = analogRead(currentPin) * 0.000805;
// Read throttle input
double throttle = analogRead(throttlePin) * 0.00488;
// Publish data to Adafruit IO dashboard
publishData(voltage, current, throttle);
// Wait for a few seconds before reading values again
delay(5000);
}
void buttonISR() {
// Determine current position of servo
int currentPosition = servo.read();
// Move servo to next position
if (currentPosition == position1) {
servo.write(position2);
} else if (currentPosition == position2) {
servo.write(position3);
} else if (currentPosition == position3) {
servo.write(position1);
}
}
Comments
Please log in or sign up to comment.