sensor selector

hi, i am working on glove assessment for hand fingers using arduino uno with two flex sensor,
i start by using just one sensor and i map its reading values from 0-100 then i map (0-100) on led bar of 10 leds which blink one by one incrementally during flexing the sensor (it works properly).
know i want some help to add my second flex sensor to the circuit and i should put an external selector which select the sensor that should be assigned to the led bar, and the change in code if there is any if statement i should use.
**NOTE: the selector should never isolate any sensor from the circuit cause i should get the values of both sensor to save it on micro SD card continuously, so what i need is to add an selector (component) which assign sensor (1 or 2) to LED bar,
and the if statement that i need for that.

thanks for help and support.

Is there any reason why you won't put the second sensor on a different pin, and decide in your code, which sensor causes which action?

i want to put the second sensor on the second pin to compare the difference angle between both sensor.
what i am thinking about know is to connect 3.3v pin to A5 and put between them a switch, so when the input at A5 high (sensor1) and if the switch is off input at A5 low (sensor2)(with an if statement in code ). is that works??

bilaldebs:
i want to put the second sensor on the second pin to compare the difference angle between both sensor.
what i am thinking about know is to connect 3.3v pin to A5 and put between them a switch, so when the input at A5 high (sensor1) and if the switch is off input at A5 low (sensor2)(with an if statement in code ). is that works??

It's difficult to understand what you are trying to do, but I'm going to take a guess, that you want to get two separate readings, from two separate sensors, then decide what to do when you check each sensor's reading.

Yes, it's possible to do that, but it's a long way around, and is more easily donw this way.

Hook sensor 1 to pin A5
Hook sensor 2 to pin A4

Then in your code.

   sensor1Value = analogRead(A5);
   sensor2Value = analogRead(A4);

   if (sensor1Value > sensor2Value) {
      // do stuff here (calculate angles and act accordingly perhaps?)
   }
   else {
      // do other stuff here  (calculate angles and act differently perhaps??)
   }

If this is not what you want to do, perhaps you could try explaining it again.

If you want a toggle switch to decide which sensor reading is reflected by the LED bar, then you should get a toggle switch and hook it up to a digital input. Without your code, it's impossible to help.

hi , thanks for support,

my project is about glove assessment for 2 finger with record of their flexion angle for one day(the record will be on micro SD card on shield), in this project user is able to switch between sensor 1 and 2 which are fixed on his fingers to see the progress done to reach the angle needed ,flag is given by led which blinks incrementally one by one(led 4-13) , also.individuation is calculated the difference between reading values of the 2 sensor and flag in 4 led (led 0-3).
so what i need to do is just selection of sensor by switch which doesn't block any sensor from reading its values but just to assign the led bar to one of those 2 sensors.

int flex1Pin1 = 0;  
int flex2Pin2 = 1;             
void setup() {
  Serial.begin(9600);
  for (int i=0; i<14; i++){
    pinMode(i, OUTPUT); //sets the led pins 4 to 13 to output
  }

}

void loop(){
   int flex1Reading = analogRead(flex1Pin1); 
   int flex2Reading = analogRead(flex2Pin2);

  Serial.println(flex1Reading);
  Serial.println(flex2Reading);
   int flexn0to100 = map(flex1Reading, 490, 310, 0, 100);
   int flexnn0to100 = map(flex2Reading, 490, 310, 0, 100);
  Serial.println(flexn0to100);
  Serial.println(flexnn0to100);
  int difference = abs(flexn0to100 - flexnn0to100);//define comparing
  delay(250); 
 //Ensure to turn off ALL LEDs before continuing 
 for (int i=0; i<14; i++){
    digitalWrite(i, LOW); 
    //compare values of flexn and flexnn.
    if (difference < 10)
    {
      digitalWrite(0, HIGH);
    }
    else   if (difference < 20)
    {
      digitalWrite(1, HIGH);
    }
    else   if (difference < 30)
    {
      digitalWrite(2, HIGH);
    }
    else   if (difference < 40)
    {
      digitalWrite(3, HIGH);
    }
    else
    {
      digitalWrite(0, LOW);
      digitalWrite(1, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW); 
  }
 }
 /* Read the flex Level 
  Adjust the value 0 to 100 to span 4 to 13
  The values 130 and 275 may need to be widened to suit 
  the minimum and maximum flex levels being read by the 
  Analog pin */
 int flexoReading = map(flexn0to100, 0, 100, 4, 13); 
         
// Make sure the value does not go beyond 4 or 13
 int LEDnum = constrain(flexoReading, 4, 13); 
 
/*Call the blink function: this will turn the LED on for 10 milliseconds, and keep it
  off for only 1 millisecond. You can change the blink rate by changing these values,
  however, I want a quick response time when the flex sensor bends, hence the small 
  values. LEDnum determines which LED gets turned on.*/
 blink(LEDnum, 19,1);
}

// The blink function - used to turn the LEDs on and off
void blink (int LEDPin, int onTime, int offTime)
{
  // Turn the LED on                                         
 digitalWrite(LEDPin, HIGH);  
 
  // Delay so that you can see the LED go On.
 delay(onTime);
 
  // Turn the LED Off                                         
  digitalWrite(LEDPin, LOW);  
 
 // Increase this Delay if you want to see an actual blinking effect.
  delay(offTime);
}

in my code i achieved the individuation (difference btw 2 sensors) and reading and assigning one sensor to led bar.

  for (int i=0; i<14; i++){
    pinMode(i, OUTPUT); //sets the led pins 4 to 13 to output
  }

Uh, no it doesn't. It sets pins 0 through 13 to OUTPUT. Not a good idea, since pins 0 and 1 are where Serial transmits and receives.

 for (int i=0; i<14; i++){
    digitalWrite(i, LOW);

Same thing here. Not a good idea.

So, you've got the two readings. I don't know how the user commands a change in what the two sensors control, but when the user does command it, simply set a flag, then use an if statement to control whatever it is you want to control.

When you say you want to switch between two sensors using another pin, are you talking about a physical switch, or are you talking about branching to different routines in your code? I'm having great difficulty understanding your explanations.

ok i will remove leds connected to pins 0&1 and makes correction on my code (i will use only pins 3-13).

according to sensor selector: i dont mean a physical switch,but branching to different routines in code due to selector position
(for example : connecting 5v to pin A4 input with switch (on/off), and add to the code if statement - if A4 is high "switch on"
first sensor reading mapped on led bar. if A4 is low "switch off" second sensor reading mapped on led bar )

thanks for help and support.

OK.

  1. wire a switch to from pin A5 to ground
  2. set the pinMode of pin A5 to INPUT_PULLUP
  3. Make an if statement
    if (digitalRead(A5) == HIGH) {
      // map flex1Reading to LEDs
    }
    else {
      // map flex2Reading to LEDs
    }