/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255 and uses
the result to set the pulse width modulation (PWM) of an output pin.
Also prints the results to the Serial Monitor.
The circuit:
- potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
- LED connected from digital pin 9 to ground
created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/AnalogInOutSerial
*/
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int newSensorValue = 0; // value read from the pot
int newOutputValue = 0; // value output to the PWM (analog out)
//************************************************************************
// Relays alternately turn on/off when the potentiometer is past midpoint.
//************************************************************************
int newRelayRight = 2;
int newRelayLeft = 3;
int oldRelayRight, oldRelayLeft, oldOutputValue, oldSensorValue;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
//************************************************************************
// Relays
//************************************************************************
pinMode(newRelayRight, OUTPUT);
pinMode(newRelayLeft, OUTPUT);
welcome();
}
void loop() {
// read the analog in value:
newSensorValue = analogRead(analogInPin);
// map sersorValue to the range of the analog out:
newOutputValue = map(newSensorValue, 0, 1023, 0, 255);
//*******************************************************************************
// LEFT relay ON if sensor < 480, RIGHT relay ON if sensor > 530, BOTH OFF 480 < relay < 530
//*******************************************************************************
if (newSensorValue < 480) {
digitalWrite(newRelayRight, LOW);
digitalWrite(newRelayLeft, HIGH);
} else if (newSensorValue > 530) {
digitalWrite(newRelayRight, HIGH);
digitalWrite(newRelayLeft, LOW);
} else { // deadband
digitalWrite(newRelayRight, LOW);
digitalWrite(newRelayLeft, LOW);
}
if (newSensorValue) { // the first sensor reading is probably zero... do not show zero
// this checks the values and only if one of them changes will data be sent to the serial monitor
if ((oldSensorValue != newSensorValue) ||
(oldOutputValue != newOutputValue) ||
(oldRelayLeft != newRelayLeft) ||
(oldRelayRight != newRelayRight)) {
// change the analog out value:
analogWrite(analogOutPin, newOutputValue);
// print the results to the Serial Monitor...
Serial.print("Analog input = ");
spacePad(newSensorValue);
Serial.print(newSensorValue);
Serial.print("\t PWM output = ");
spacePad(newOutputValue);
Serial.print(newOutputValue);
Serial.print("\t Relay statuss = ");
Serial.print(digitalRead(newRelayLeft));
Serial.print(" | ");
Serial.println(digitalRead(newRelayRight));
}
// store old values for checking if any changes on the next loop
oldSensorValue = newSensorValue;
oldOutputValue = newOutputValue;
oldRelayLeft = newRelayLeft;
oldRelayRight = newRelayRight;
}
}
void spacePad(int value) { // for column formatting of three digits
if (value < 100)
Serial.print(" ");
if (value < 10)
Serial.print(" ");
}
void welcome() {
Serial.println("* Adjust Analog In. * Only changes will be displayed. * Relays alternate states at midpoint.");
}PWM OUT
RELAY (L)
RELAY (R)
ANALOG IN