Go Down

Topic: Limit on sensors on Uno? (Read 1 time) previous topic - next topic

mildish

Sorry, I'm a super newbie in over my head here. This is probably a stupid question, but is there a limit to the amount of sensors I can use at one time on this board? I will post some code below, but I have ten reed switches on pins 1-10. For some reason, It will not read the sensor on pin 1 or pin 10... in constantly thinks that they are 'HIGH'. Is this just bad connections, a short, or something more. I didn't know if the fact that arduino was 8 bit was the reason. Just seems odd that I stopped at 8. Below is the code. Sorry for the stupid question. It won't even work with simple code like this:

Code: [Select]


#define LED 13 // pin for LED
#define BUTTON1 1 // the input pin for the button
#define BUTTON2 2 // the input pin for the button
#define BUTTON3 3 // the input pin for the button
#define BUTTON4 4 // the input pin for the button
#define BUTTON5 5 // the input pin for the button
#define BUTTON6 6 // the input pin for the button
#define BUTTON7 7 // the input pin for the button
#define BUTTON8 8 // the input pin for the button
#define BUTTON9 9 // the input pin for the button
#define BUTTON10 10 // the input pin for the button

int val1 = 0; //val will be used to store the state of the input pin
int val2 = 0; //val will be used to store the state of the input pin
int val3 = 0; //val will be used to store the state of the input pin
int val4 = 0; //val will be used to store the state of the input pin
int val5 = 0; //val will be used to store the state of the input pin
int val6 = 0; //val will be used to store the state of the input pin
int val7 = 0; //val will be used to store the state of the input pin
int val8 = 0; //val will be used to store the state of the input pin
int val9 = 0; //val will be used to store the state of the input pin
int val10 = 0; //val will be used to store the state of the input pin


void setup() {
  pinMode(LED, OUTPUT); //sets led as an output
  pinMode(BUTTON1, INPUT); //button is input
  pinMode(BUTTON2, INPUT); //button is input
  pinMode(BUTTON3, INPUT); //button is input
  pinMode(BUTTON4, INPUT); //button is input
  pinMode(BUTTON5, INPUT); //button is input
  pinMode(BUTTON6, INPUT); //button is input
  pinMode(BUTTON7, INPUT); //button is input
  pinMode(BUTTON8, INPUT); //button is input
  pinMode(BUTTON9, INPUT); //button is input
  pinMode(BUTTON10, INPUT); //button is input
}

void loop() {
val1 = digitalRead(BUTTON1); // read input value and store it
val2 = digitalRead(BUTTON2); // read input value and store it
val3 = digitalRead(BUTTON3); // read input value and store it
val4 = digitalRead(BUTTON4); // read input value and store it
val5 = digitalRead(BUTTON5); // read input value and store it
val6 = digitalRead(BUTTON6); // read input value and store it
val7 = digitalRead(BUTTON7); // read input value and store it
val8 = digitalRead(BUTTON8); // read input value and store it
val9 = digitalRead(BUTTON9); // read input value and store it
val10 = digitalRead(BUTTON10); // read input value and store it

//execute code based on the value

   if (val1 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
 
   if (val2 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
   
   if (val3 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
   
   if (val4 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
   
   if (val5 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
   
   if (val6 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
   
   if (val7 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
   
   if (val8 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
   
   if (val9 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }
   
   if (val10 == HIGH) {
     digitalWrite(LED, HIGH); // if button is pressed, turn on LED
   } else {
     digitalWrite(LED, LOW); // no press, LEd off
   }

}




RuggedCircuits

First, you need to stay away from digital pins 0 and 1. They are used for serial (USB) communication to the PC and will interfere with what you are doing.

Second, how do you have your switches connected? The typical way to do so is to have the switch make a contact between the digital pin and ground. That forces the pin to 0V. But you need to also have a way to force the pin to 5V when the switch *isn't* pressed. That's what the AVR built-in pullup-resistors are for. They are enabled by writing 1 to the digital input pin:

Code: [Select]
pinMode(BUTTON2, INPUT); digitalWrite(BUTTON2, HIGH);

I don't know what's going on with pin 10. It should work just like all the others. Maybe check your wiring, your wires, your switch....

Your code is also strange in that you are going to be setting the LED's low most of the time, except for the one brief interval in which you set the LED high, right before checking the next button, which will set it low again. I'm thinking you want something more like....

Code: [Select]

if ( (val2==HIGH) || (val3==HIGH) || ...etc... || (val10==HIGH) ) {
  digitalWrite(LED, HIGH);
} else {
  digitalWrite(LED, LOW);
}


--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

jezuz

I agree with rugged - button 10 likely has a wiring issue, the code seems to be identical for all buttons and without any mistakes.

As a side note - this project would be a good way to learn about arrays and for loops - makes that if statements block much cleaner.

Magician

Code: [Select]
Sorry, I'm a super newbie in over my head here. This is probably a stupid question, but is there a limit to the amount of sensors I can use at one time on this board?

Answer : limit only hardware (19/20 on UNO and they say 64 on other boards, looks in data sheet)I

will post some code below, but I have ten reed switches on pins 1-10. For some reason, It will not read the sensor on pin 1 or pin 10... in constantly thinks that they are 'HIGH'.

Please, provide a schematic.

Is this just bad connections, a short, or something more. I didn't know if the fact that arduino was 8 bit was the reason.

wrong supposition,  yes chip 8-bits, but its' doesn mean it can calculate billion numbers,
CPU is not limited,.

Just seems odd that I stopped at 8. Below is the code. Sorry for the stupid question. It won't even work with simple code like this:

Go Up