/*
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:
- Jostick VERT connected to analog pin A0.
Joystick HORZ connected to analog pin A1.
VCC and GND of the Joystick go to +5V and ground
5V relay module 1 SIG is connected to output Pin 0
- 5V relay module 2 SIG is connected to output Pin 1
Created 24 Sept 2023
by Jon Vargo
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInOutSerial
*/
// These constants won't change. They're used to give names to the pins used:
const int analogInPin0 = A0; // Analog input pin that the potentiometer is attached to
const int analogInPin1 = A1; // Analog input pin that the potentiometer is attached to
const int pinMode1delay = 0; // Analog output pin that realy 1 is attached to
const int digitalOutPin = 1; // Analog output pin that relay 2 is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
//*****************************************************************************
// Relays turn on when the potentiometer is past midpoint
int relay = 6;
int oldRelays, oldOutputValue, OldSensorValue;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
//************************************************************************
// Relays
//************************************************************************
pinMode0(relay, OUTPUT);
pinMode1(relay, OUTPUT);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin0);
// map it to the range of the analog out:
outputValue = map(sensorValue, 1023, 0, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
sensorValue = analogRead(analogInPin1);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the Serial Monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
//*******************************************************************************
// relays OFF if sensor = 512, relays ON if sensor > 530, < 494
//*******************************************************************************
if (sensorValue < 400) {
digitalWrite(relays, HIGH); }
if (sensorValue > 400) {
digitalWrite(relays, LOW);}
if (sensorValue > 700) {
digitalWrite(relays, HIGH);}
}