Hi all,
I'm still really new to this ardino malarkey, I've spent a bit of time getting to grips with some of the basics.
I've managed to get as far as I can with some code, but I cant quite get my head around something.
Excuse the code, I'm keeping it as basic to me as I can until I understand everything better. I'm currently using a Uno to prototype what I want to do, and will be using a nano in the final project, hence some extra lines of code, (8 output and inputs)
What I am trying to achieve is:
Independently switch 4 relays on and off from a voltage from 4 independent voltage sensors.
So when voltage is read from say pin 1, relay 4 is active, but relays 1,2 and 3 are not.
Then when voltage is read from pin 4, relay 2 is active but relays 1,3 and 4 are not. etc
i think its a case of some if, else if, and else statements, but i may be wrong.
see below for my attempt at code, and help point me in the right direction.
/*
* Control for 8 relays to control 8 LED strips.
* 8 relays triggered to shut 7 relays open from an indepentant voltage input from a voltage sensor on analog pins.
* Relays closed in normal state, so lighting on. signal switches LED off
*/
const int analogPin1 = A0; // read voltage from pin 0
const int analogPin2 = A1; // read voltage from pin 1
const int analogPin3 = A2; // read voltage from pin 2
const int analogPin4 = A3; // read voltage from pin 3
const int analogPin5 = A4; // read voltage from pin 4
const int analogPin6 = A5; // read voltage from pin 5
const int analogPin7 = A6; // read voltage from pin 6
const int analogPin8 = A7; // read voltage from pin 7
const int threshold = 4; // input voltage 4v or more
void setup()
{
pinMode(13, OUTPUT);// sets the digital pin 13 as output
pinMode(12, OUTPUT);// sets the digital pin 12 as output
pinMode(11, OUTPUT);// sets the digital pin 11 as output
pinMode(10, OUTPUT);// sets the digital pin 10 as output
pinMode(9, OUTPUT);// sets the digital pin 09 as output
pinMode(8, OUTPUT);// sets the digital pin 08 as output
pinMode(7, OUTPUT);// sets the digital pin 07 as output
pinMode(6, OUTPUT);// sets the digital pin 06 as output
Serial.begin(9600); // setup serial
}
void loop() {
// read the sensor:
const int analogValue = analogRead(analogPin1);
const int analogValue2= analogRead(analogPin2);
// if the analog value is 0, keep all LEDs on, is analog value detected at sensor, turn off 7 bays keep one on.
if (analogValue < threshold) {
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
} else {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
}
if (analogValue2 < threshold) {
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
} else {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
}
// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
}
Cheers!


