Need help with how to write code for multiple switches that output PWM and also control relays simultaneously.
Did a project recently that consisted of an Arduino UNO Rev 3, an analog thumb wheel switch, and two 5V relay modules.
The analog thumb wheel switch connected to input A0 produced a PWM signal on output pin 9 and also controlled a pair of 5V relays connected to output pin 6.
Attempted unsuccessfully modify that code to add additional switches and relays. Also researched Arduino libraries for some examples I could try and wasn’t able to find anything similar.
Is there anything out there to help me learn how to add switches and relays to a project?
This is the code from my recent project I tried to modify to add additional switch and relay. I tried multiple different ways to write the code but was met with coding errors.
/*
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 through 220 ohm resistor
created 29 Dec. 2008
modified 3 Sept 2023
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 analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
const int digitalOutPin = 6;// Analog output pin that relays are 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 relays = 6;
int oldRelays, oldOuputValue, oldSensorValue;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// ****************************************************************************************
// Relays
// ****************************************************************************************
pinMode(relays, OUTPUT);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// 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 ON if sensor < 400, > 700, relays OFF if sensor > 400,
// ********************************************************************************************
if (sensorValue > 525) {
digitalWrite(relays, HIGH);
} if (sensorValue < 475) {
digitalWrite(relays, HIGH);
} else if (sensorValue >= 475 && sensorValue <= 525) {
digitalWrite(relays, LOW);}
}
Yeah coding errors, very friendly always want to meet up and stuff.
Would a crude but effective solution be to duplicate in its entirety the current working device?
I mean to ask if you want another "channel" to do exactly the same thing with exactly the same input and output.
Go ahead and post an attempt, or dexribe in more detail what you tried.
Making N identical things is conceptually very easy. A naive beginners approach would be to copy relevant code sections, and edit it to use different pins.
That gets old and is prone to error after about 2\one or two copies.
A more common way would be to use your code as a model, and replace the scalar variables with array variables.
not sure what you're looking for. you asked about switches but posted code that monitored analog inputs
the following shows how multiple digital inputs can be monitored and control associated outputs. this could easily be modified to monitor analog inputs
// check multiple buttons and toggle LEDs
enum { Off = HIGH, On = LOW };
byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT sizeof(pinsBut)
byte butState [N_BUT];
// -----------------------------------------------------------------------------
int
chkButtons ()
{
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
byte but = digitalRead (pinsBut [n]);
if (butState [n] != but) {
butState [n] = but;
delay (10); // debounce
if (On == but)
return n;
}
}
return -1;
}
// -----------------------------------------------------------------------------
void
loop ()
{
switch (chkButtons ()) {
case 2:
digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
break;
case 1:
digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
break;
case 0:
digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
break;
}
}
// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (9600);
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
pinMode (pinsBut [n], INPUT_PULLUP);
butState [n] = digitalRead (pinsBut [n]);
}
for (unsigned n = 0; n < sizeof(pinsLed); n++) {
digitalWrite (pinsLed [n], Off);
pinMode (pinsLed [n], OUTPUT);
}
}