Go Down

Topic: Comparing variables and choosing highest (Read 119 times) previous topic - next topic

ac21

Code: [Select]

void switchrx(){
     if ((rx1rssi > rx2rssi + 10) || (rx1rssi > rx3rssi + 10)) {
              Serial.println("SWITCH to 1 ================" );
              digitalWrite(rx2out, LOW); 
              digitalWrite(rx3out, LOW);               
              delay(5);             
              digitalWrite(rx1out, HIGH);   
              delay(1000);
   }
   else if ((rx2rssi > rx1rssi + 10) || (rx2rssi > rx3rssi + 10)) {
              Serial.println("SWITCH to 2 ================" );   
              digitalWrite(rx1out, LOW); 
              digitalWrite(rx3out, LOW);               
              delay(5);             
              digitalWrite(rx2out, HIGH);   
              delay(1000);
    }
     
   else if ((rx3rssi > rx2rssi + 10) || (rx3rssi > rx1rssi + 10)) {
              Serial.println("SWITCH to 3 ================" );   
              digitalWrite(rx1out, LOW); 
              digitalWrite(rx2out, LOW);               
              delay(5);             
              digitalWrite(rx3out, HIGH);   
              delay(1000);
   
   }
   
  }   


Imagine rx1rssi, 2 and 3 are buckets of water and if one of those buckets gets filled with water more then 10 oz then the others I want dump that bucket.

above is the code i have, but how it is now if rx1rssi is more then rx2rssi, rx1rssi will dump without caring what rx3rssi is.    How can i compare all three?

ac21

This works, is there a better way to do it?

Code: [Select]

void switchrx(){

if (rx1rssi > rx2rssi + 10)
{if (rx1rssi > rx3rssi + 10){
              Serial.println("SWITCH to 1 ================" );
              digitalWrite(rx2out, LOW); 
              digitalWrite(rx3out, LOW);               
              delay(5);             
              digitalWrite(rx1out, HIGH);   
              delay(1000); 
 
  }}

if (rx2rssi > rx1rssi + 10)
{if (rx2rssi > rx3rssi + 10){
              Serial.println("SWITCH to 2 ================" );
              digitalWrite(rx1out, LOW); 
              digitalWrite(rx3out, LOW);               
              delay(5);             
              digitalWrite(rx2out, HIGH);   
              delay(1000); 
 
  }}

if (rx3rssi > rx2rssi + 10)
{if (rx3rssi > rx1rssi + 10){
              Serial.println("SWITCH to 3 ================" );
              digitalWrite(rx2out, LOW); 
              digitalWrite(rx1out, LOW);               
              delay(5);             
              digitalWrite(rx3out, HIGH);   
              delay(1000); 
 
  }} 

spirit

Quote
This works, is there a better way to do it?
your previous code, only you need to replace the || OR with a && AND sign.

UKHeliBob

How about putting the RSSI values in an array and iterating through it to find the highest ?

A diversity receiver I imagine ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ac21

your previous code, only you need to replace the || OR with a && AND sign.
there was some issue with that I also don't see it now.


How about putting the RSSI values in an array and iterating through it to find the highest ?

A diversity receiver I imagine ?
I'm not familiar with working with arrays.

Yes I'm using three RC305(rx5808) with the rssi pin tapped into. If you are interested I can provide more detail.   

Delta_G

I'm not familiar with working with arrays.
Then what a great time to learn.  It's not terribly hard.  Use google to find a tutorial on arrays in C++.  There are plenty.  Read a few of them. 
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

ac21

Then what a great time to learn.  It's not terribly hard.  Use google to find a tutorial on arrays in C++.  There are plenty.  Read a few of them. 
I was gonna get to it but I guess now is a great time :smiley-neutral:
Thank you Sir

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy