Banging my head on a wall

NMNeil

Oct 3, 2014
111
Joined
Oct 3, 2014
Messages
111
I have designed the electronics for the ignition system for my old 49 tractor, but have come up against a brick wall with the coding for the Arduino.
It simply didn't work, and after repeatedly simplifying the code to find the problem, I found that I can never get a valid input from the pin.
Here's the code I trimmed it down to:

void setup(){
Serial.begin(9600);
pinMode(7, INPUT);
}

void(loop){
int pinState = (digitalRead, 7);
Serial.print(pinState);
delay(1000);
}

When I run it I get a row of 7's on the serial monitor. Tied pin 7 to ground to cut out any stray signals and got a row of 7's, tied it to 5 volts and got the same row of 7's.
Changed the code so that pin 3 was the input and got a row of 3's !!!!!!
Replaced the Atmega 328 on the board but got the exact same results.

What am I doing wrong?
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
For giggles, perhaps you should take a look at the "int pinState= (digitalRead, 7);" line
Set pinState to a static number or string and try again...
Alternatively perhaps using a digital read like above is what is causing issues...
int pinState = digitalRead(7);

I would suggest both.. but remember that the serial output will merely be 1's or 0's depending on the state of the pin.
 

NMNeil

Oct 3, 2014
111
Joined
Oct 3, 2014
Messages
111
Thanks for the reply, although I'm not sure what you mean.
I am generating a signal from a reflective optical sensor, and using a 10K pulldown resistor I send either a high(4.3 volts) or low (0 volts) to pin 7. I want pin 7 to read this signal and output either 0 or 1. But whatever changes I make to the code all I ever get is a number other than 0 or 1.
Be gentle I'm new to this electrickery stuff
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Thanks for the reply, although I'm not sure what you mean.
I am generating a signal from a reflective optical sensor, and using a 10K pulldown resistor I send either a high(4.3 volts) or low (0 volts) to pin 7. I want pin 7 to read this signal and output either 0 or 1. But whatever changes I make to the code all I ever get is a number other than 0 or 1.
Be gentle I'm new to this electrickery stuff
Take a look at the sample code you posted. specifically this line:
int pinState = (digitalRead, 7);

Make note of where the brackets are.
The Arduino documentation shows the syntax for digitalRead is:
var = digitalRead(pin);
So that line should read:
int pinState = digitalRead(7);

To ensure things are working as expected though... you should probably do this first:
int pinState = 37;

If you get 37 on your serial terminal, then the send is working correctly.
Switch back to the corrected line I posted and try again.
 
Top