reading voltage and if statements

Gang...

I'm quite new to this, the last time I coded was when I used applesoft, so that's been 45 years ago,

Here's what I want to do.

I have eight voltage dividers. The voltage run from 0 to 5, each one of the eight will have a different range of voltages. Say divider one is .9 to 1.2V and divider is 1.3 to 2V and divider 3 is 2.2 to 3V and so on.

It took me hours to produce this little bit of code. Literally, hours. it works with only one divider.

right now, I'm at a lost.

The problem I can't seem to solve is how to determine the voltage on any of the eight dividers because even the best resistors in the world will drift. So, divider one may go from .9 to 1.2 V.
How do I check for that?

Then if I do another if (maybe a if else?)
and look at another divider, well shucks, divider 1 is already greater than 1.2 and will prove true. I can't have two actions true at the same time.

so in a nut shell;

how do i measure voltage on A0 when it varies from .9 to 1.2 V and have a return of true? Think of it as a window that's opened a tiny bit. Whatever comes in, between .9 and 1.2V must be true, yet ignore it when the voltage on A0 is higher than 1.2 V
And do it again for seven more times, each one will be looking for a different (make that higher) voltage than the other ones

there is some fluff in my code, mostly to give me an indication that what I'm asking it what it's doing like light an LED if the value is true. That part of the code isn't needed, however I wanted to prove the code actually did something.

I changed the value in my if statement and re run the sketch to verify that the condition changed. i.e. changed the if statement value to 4 so it won't return true

I sure could use some help

please don't laugh at my coding...It's all new to me

mike

antenna_Volt_read.ino (2.14 KB)

how do i measure voltage on A0 when it varies from .9 to 1.2 V and have a return of true? Think of it as a window that's opened a tiny bit. Whatever comes in, between .9 and 1.2V must be true, yet ignore it when the voltage on A0 is higher than 1.2 V

You can't do that, exactly the way you describe.

The voltage on the pin is reported as a ratio of the reference voltage of the board, in the range 0 to 1023. Some value corresponds to 0.9V on the pin. Some value corresponds to 1.2V. If the reference voltage is rock steady, you can determine what those values should be.

Then, you can use an if statement to see if the value is in the range defined by the low end value and the high end value.

Suppose that the rock steady voltage is 5.0V. Then, 0.9V on the analog pin will return a value of 184. 1.2V would return a value of 245.

int someValue = analogRead(somePin);
if(someValue >= 184 && someValue <=245)
{
   // voltage IS between 0.9 and 1.2
   // "return" true
}
else
{
   // voltage is either lower than 0.9 or higher than 1.2
   // "return" false
}

You can choose to use, or ignore, any reading, based on it being in a range, or not.

Ah poop!

Nothing is ever simple is it?

Nothing is ever simple is it?

On a scale of 1 to 10, that rates about a 0.02.

qrper:
I have eight voltage dividers. The voltage run from 0 to 5, each one of the eight will have a different range of voltages. Say divider one is .9 to 1.2V and divider is 1.3 to 2V and divider 3 is 2.2 to 3V and so on.

Maybe explain first why you have eight dividers. What are you reading.
What kind of Arduino are you using. The Uno only has six analogue inputs.

If all voltage spans were the same, then you could use 'map' to compress to eight spans. e.g. 0-1023 to 0-7.
Then use e.g. 'switch case' to do something with the eight conditions.
Leo..

The project is a remote antenna switch. A rotary switch selects one of eight relays by sending +12 votls down a cable to the antenna switch 200+ feet down stream.

When any one of the 8 relays is selected, the Ardinuo willl read that voltage and display what antenna
has been selected.

That is done by turning on a 78l08 regulator on each antenna relay, with a voltage divider on its output. That output, depending on the switch, will vary from 0-.7V to 5 V. Each output from the 8 relays are "ORed" together so only one AD pin is used. I used the regulator so I can discard any variance in wire runs and or operating voltage. So, every time antenna 5 is selected, that particular 78L08 is turn on as well as the two relays on the antenna tower. The voltage divider is set so 1.8 to 2.2 V is read by the ardunio and Antenna 5 "LOOP ANTENNA" is displayed on the LCD.

There is a 100mA current limit on the relays and should one of the 16 relays fail, there are two per antenna port ,or a break in the cable, an LED is lit on the switch board. That output will also go to the ardunio and will clear the display and at the same time announce that there has been a fault detected.

Coding is not my strong point and I freely admit those that know how to do so are truly gifted.

I struggle.

And before anyone jumps my bones about how it I'm doing it and I should have done it this or that way, this works for me. Yes, I could used zener diodes, could use a A/D pin, could have .....sometimes one has to shoot the engineer to get the project done

You don't need to convert to a voltage, what are the ADC results (0 to 1023) for each divider?

That depends on which switch is selected. Each one has a different voltage.

The attached photo shows my test bed. On the left, you'll see the four 7808 regulators and the relays. The relay board is a bit big, so I'm only testing with the relay plugged into the prototype board.

The LCD's top line returns the result of the voltage divider, and as you can see, it's on antenna 3 because the voltage from the external voltage divider is within the window of the IF statement.

The second photo shows the current limiting board piggyback onto the switch board, and the third photo shows how they're stacked. That yellow LED is on because no relays are attached and that's my fault LED. That signal will be sent to the ardunio and will display an error message.

The switch board was going to have 8 LEDs to show what antenna port was selected, and then I had a brain fart and wanted to try using the ardunio.

I'll need to redesign the switch board so it will hold a Nano and the LCD. I'm not sure I'll have enough room to get the switch into the panel.

More problems...

That depends on which switch is selected. Each one has a different voltage.

I understand that, let me rephrase the question.
If relay #1 is on, what is the ADC reading?
If relay #2 is on, what is the ADC reading?
...
...
...
If relay #8 is on, what is the ADC reading?

switch 1 is from .1 to .66 volts
switch 2 is from .70 to 1.2 volts

switch 8 reading 5 volts.

I haven't measure it but it suppose to be about .7 volt per position. I'll have to check and get back

mike

seems like a sound project.

as the arduino is bringing in a reading of 0-1023 edgemoron has already pointed out that the conversion to voltage is for your benefit rather than the arduino. Even if you like to see the voltage its better to compare in the raw number as the calculation to voltage may cause a little drift due to the maths. You can then calculate the results back into voltage for the lcd display.

You could even program the arduino to test all 8 channels during start up (check for fault conditions) then have the arduino set the ADC numbers based on the test results. As long as none of the readings overlap (hardware side) it shouldn't be a problem.

thanks!

The voltage dividers are adjustable, so I can tweak each one for the sweet spot.

The photo that shows the voltage as the ports are selected is for testing only. It will not be visible. I'm thinking of putting in a few lines of code to allow the voltages to be display for setup purpose.

I'm barely have enough smarts to code this thing and have spent hours upon hours looking and reading how to do it on the 'net.

qrper:
I'm barely have enough smarts to code this thing and have spent hours upon hours looking and reading how to do it on the 'net.

you have a picture of a lcd reading voltage and displaying the channel. You seem to be doing fine.

When you are ready post the code and explain what it does and what you want it to do and people will happily point you In the right direction.

I don't see why you can't use the MAP function (see post#4).
Then you have eight numbers for the eight conditions.
Basic principle attached.
Leo..

void setup() {
  Serial.begin(9600);
}

void loop() {
  byte relayNumber = map(analogRead(0), 0, 1024, 1, 9);
  Serial.print("Relay: ");
  Serial.println(relayNumber);
}

Only 0.04 volts between sw 1 and sw 2? Thats gonna be troublesome. 5V / 8 = 0.625V / step, you should space your dividers close to that. 0.625V = 127 ADC counts, that would give you plenty of "wiggle" room between switch points.
EDIT: Lets make that 9 steps at 0.5555V / step and 113 ADC counts / step. Step 1 = no switches.

how do i measure voltage on A0 when it varies from .9 to 1.2 V and have a return of true?

Well, assuming that AREF is 5v,

// 5v reads as a value of 1023
const float ANALOG_2_V = 5.0 / 1023.0;

boolean foo() {
  float v = analogRead(A0) * ANALOG_2_V;
  return v >= .9 && v <= 1.2;
}

Wawa:
I don't see why you can't use the MAP function (see post#4).
Then you have eight numbers for the eight conditions.
Basic principle attached.
Leo..

void setup() {

Serial.begin(9600);
}

void loop() {
 byte relayNumber = map(analogRead(0), 0, 1024, 1, 9);
 Serial.print("Relay: ");
 Serial.println(relayNumber);
}

That's quite simple! I don't have a clue as to how to do that. This is my first Ardunio project, first time at coding a sketch..

edgemoron:
Only 0.04 volts between sw 1 and sw 2? Thats gonna be troublesome. 5V / 8 = 0.625V / step, you should space your dividers close to that. 0.625V = 127 ADC counts, that would give you plenty of "wiggle" room between switch points.
EDIT: Lets make that 9 steps at 0.5555V / step and 113 ADC counts / step. Step 1 = no switches.

It was late when I posted that. The computer with the sketch is down in the shack, and I guessed at the values from memory. I know it's more than .5 V and less than a volt.

gpop1:
you have a picture of a lcd reading voltage and displaying the channel. You seem to be doing fine.

When you are ready post the code and explain what it does and what you want it to do and people will happily point you In the right direction.

I'll post it in a few minutes.

Now everyone take into account this is my FIRST Ardunio sketch.

remote_switch_V3.3.ino (4.58 KB)

this might be wrong but it compiles

/*/remote switch display
  vers 3.2  26 August 2017
  Voltage read on A1 from antenna remote
  Fault alarm pin 4
  Test switch on pin 9
*/
// Voltage read on A1 from antenna remote
//
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(8,9,10,11,12,13); DONT NEED THIS NOW WITH I2C LCD


#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // F Malpartida's NewLiquidCrystal library
#define I2C_ADDR 0x27 // Define I2C Address for controller
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define BACKLIGHT 3
#define fault 4       //THIS IS THE PIN THAT LOOKS FOR THE fault LED from switch controller. H = ok L = fault
#define test 9       // switch to turn on voltage readings for setting display

LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

int input2;
int input ;
int antenna;
int radios;
int testPin = 9;     //digital pin 9 for switch to show volts on display
int antennaNumber = 0;

void setup() {
  lcd.setBacklightPin(3, POSITIVE);
  lcd.begin(16, 2);
  lcd.clear();                        //clears any junk on the screen
  delay(2000);                        //wait 2 seconds
  lcd.print(" Antenna Switch");       //a quick splash
  delay(4000);                      //take a look for four seconds


  pinMode(4, INPUT_PULLUP); // Fault input  on digital pin 4
  pinMode(testPin, INPUT_PULLUP);  //select for voltage readings on LCD

}

void loop() {
  //input = analogRead(A1);              //this is the input for the antenna relays
  input2 = analogRead(A2);             //input for the transceivers
  //antenna = (input / 129) + 1;
  radios = (input2 / 129) + 1;

  show();
}

//****Functions****

void show() {

  lcd.clear();//got to work out how to deal with this to stop screen flicker

  if (digitalRead(testPin) == LOW) { //when pin is low, the Display will show AD voltage on LCD

    lcd.clear();//got to work out how to deal with this to stop screen flicker
    lcd.setCursor(0, 0);                   //returns cursor to first line
    lcd.print("#");                           //only way I know of to get both the voltage and antenna port on the LCD
    input = analogRead(A1);   //wake pin
    antennaNumber = map(analogRead(A1), 0, 1024, 1, 9);//read pin
    lcd.print(antennaNumber); //was to put these IF statements here or the line blinked.
    printAntenna(antennaNumber);
    lcd.setCursor(0, 1);
    lcd.print(input / 204.6);
    lcd.print(" volts input");
    delay(500);                               // reduce flicker
  } else if (digitalRead(fault) == LOW) { //Fault and test both active?

    //this tests to see if the antenna switch has a fault
    lcd.setCursor(0, 0);                   //sets cursor to second line
    lcd.print("SWITCH FAULT! ");           //prints to lcd that something is wrong
    lcd.setCursor(0, 1);
    lcd.print("Do NOT transmit!");
  } else {//test pin and fault pin are high to get here
    //this selects the antenna and displays the results on the LCD

    lcd.setCursor(0, 0);                  //returns cursor to first line
    lcd.print("#");
    lcd.print(antennaNumber);                   //prints out the number of the switch
    lcd.print(" ");
    printAntenna(antennaNumber);
    //Let's select a radio by reading the voltage on analog pin 2

    lcd.setCursor(0, 1);                  //places this info on the second line

    // lets show what radios are connected to the other switch

    lcd.print("#");
    lcd.print(radios);
    if (radios == 1)lcd.print("TenTec OMNI 7");
    if (radios == 2)lcd.print(" Yaesu FT-450D");
    if (radios == 3)lcd.print(" TenTec Jupiter");
    if (radios == 4)lcd.print(" TenTec Omni V");
    if (radios == 5)lcd.print(" TenTec Omni C");
    if (radios == 6)lcd.print(" Drake TR4CW");
    if (radios == 7)lcd.print(" Drake TR-5");
    if (radios == 8)lcd.print(" Yaesu FT-1200");
    delay(200);//Reduce flicker
  }

}


void printAntenna(byte show) {

  switch (show) {
    case 1:
      lcd.print(" Vertical");
      break;
    case 2:
      lcd.print(" 40 Meter DP");
      break;
    case 3:
      lcd.print(" (Yagi)");
      break;
    case 4:
      lcd.print(" (Loop)");
      break;
    case 5:
      lcd.print(" (6 meter DP)");
      break;
    case 6:
      lcd.print(" (Parabolic)");
      break;
    case 7:
      lcd.print(" (Beverage)");
      break;
    case 8:
      lcd.print(" (Dummy load)");
      break;
    default:
      lcd.print("bad reading");
      break;
  }
}

ops had to edit a cut and copy mistake