Digital Read/Write Problem

Hey guys! so this code has been giving me quite a bit of trouble and I was wondering if you could help me. This program is designed to test if pins 13, 12, or 11 are high and if it is to lower pin 7 and turn on the corresponding pin.

If you could tell me if this is done correctly that would be great!

The problem that I've been having is that as soon as I run the sketch pin 10 goes high and seven goes low, even when there is nothing going into pin 13. I've tried changing the pin, and that fixes pin ten going high, but pin seven still goes low.

Thanks for the help! :slight_smile:

int val11 = 0;
int val12 = 0;
int val13 = 0;

void setup() {
  pinMode(13, INPUT);
  pinMode(12, INPUT);
  pinMode(11, INPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, HIGH);
}

void loop() 
{ 
  val13 = digitalRead(13);
  val12 = digitalRead(12);
  val11 = digitalRead(11);
  if (val13 == HIGH)
  {
    digitalWrite(10, HIGH);
    digitalWrite(7, LOW);
  }
  if (val12 == HIGH)
  {
    digitalWrite(9, HIGH);
    digitalWrite(7, LOW);
  }
  if (val11 == HIGH)
  {
    digitalWrite(8, HIGH);
    digitalWrite(7, LOW);
  }
}

What is connected to the pins you are reading? Are they floating? If so, they might read HIGH one second and LOW the next. Nothing connected means the pin can read whatever static it pics up from the environment.

Delta_G:
What is connected to the pins you are reading? Are they floating? If so, they might read HIGH one second and LOW the next. Nothing connected means the pin can read whatever static it pics up from the environment.

Each of the three pins has the same hookup, so I'll use pin 13 as an example.

To start, we have a MOSFET with 5V on the source, and pin 7 on the gate. The drain hooks to a momentary switch which then feeds into pin 13.

AnAverageName:
To start, we have a MOSFET with 5V on the source, and pin 7 on the gate. The drain hooks to a momentary switch which then feeds into pin 13.

Can you draw a picture of that? So when the switch isn't made, what's feeding into 13 then?

What is this circuit supposed to do?

Transistors and MOSFETs etc are for outputs not inputs!

Use Serial to print out the states of the pins.

Mark

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What is your electronics, programming, arduino, hardware experience?

Tom… :slight_smile:

Here's the picture! :slight_smile:

When the switch isn't made, nothing is going into 13.

The circuit is supposed to wait until there is an input on one of the pins, and then turn off pin 7 to block any other input from coming in.

Transistors and MOSFETs etc are for outputs not inputs!

Use Serial to print out the states of the pins.

Mark

Could you elaborate on that a bit, especially the outputs/inputs part? Thanks!

What is your electronics, programming, arduino, hardware experience?

My entire experience consists of three weeks at Duke University's three-week summer EE class for high school students, a high school C++ course, and an ongoing internship at BYU's Electro-Optics lab with a focus on Electro-Holography.

Once again thank you all for the help! I really appreciate it!

You can block the other input in software, and should. You're using three components and an extra pin to do what you could do with a single byte of memory and a few lines of simple code code. (ie, byte pressed=0; at start, and then test for that when you read the pins, and if it's not 0, don't look at the pins - and when you process a button press event, you set pressed=1)

Regardless:

You need a pulldown resistor on each button pin - when the button isn't pushed, there is currently nothing connected to the pin, and it's an input, so it's floating - it will transition between high, low, and somewhere in between due to local electromagnetic fields (acting as an antenna). 10k is a good default value for pulldown resistors.

However, if you reverse it, and have the switch connect the pin to ground, you can use the chip's internal pullup, by doing pinMode(pin,INPUT_PULLUP) - now the pin will read high unless the button is pressed, connecting it to ground. (so you have to invert the logic in your code, too)

DrAzzy:
You can block the other input in software, and should. You're using three components and an extra pin to do what you could do with a single byte of memory and a few lines of simple code code. (ie, byte pressed=0; at start, and then test for that when you read the pins, and if it's not 0, don't look at the pins - and when you process a button press event, you set pressed=1)

Regardless:

You need a pulldown resistor on each button pin - when the button isn't pushed, there is currently nothing connected to the pin, and it's an input, so it's floating - it will transition between high, low, and somewhere in between due to local electromagnetic fields (acting as an antenna). 10k is a good default value for pulldown resistors.

However, if you reverse it, and have the switch connect the pin to ground, you can use the chip's internal pullup, by doing pinMode(pin,INPUT_PULLUP) - now the pin will read high unless the button is pressed, connecting it to ground. (so you have to invert the logic in your code, too)

Great, thanks for the advice! I'll try it tomorrow! Also, a pulldown resistor would go right after the switch and to ground, correct?

Yes, a pulldown would go from the arduino-side of the switch to ground.