Notifications
Clear all

Need Arduino programming help

38 Posts
11 Users
1 Reactions
8,450 Views
(@bits2bots)
Member
Joined: 1 year ago
Posts: 30
 
I found the problem. digitalRead does not read a pin number rather it reads the function of that pin. In other words pin 10 is D7 on the Nano. Pin 13 is D10.  const int inPin = 10 in my code refers to functional D10 not physical pin 10, which is D7. ... oopos!!
 

constint inPin =10; //D10 not pin 10 is what the compiler recognizes
int pinState;
 
voidsetup() {
  // put your setup code here, to run once:
Serial.begin (9600);
pinMode (inPin, INPUT_PULLUP); //use internal pullup
 //pinMode (11, OUTPUT);

}

voidloop() {
 
pinState =digitalRead(inPin); // reading D10 not pin 10
Serial.println (pinState);
delay(2000);
}
 
This code works as expected!


   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 444
 

@dronebot-workshop 

Posted by: @dronebot-workshop

Try just printing the value of pinState. You declared it as an integer, but you are looking at it to be "HIGH" like a boolean.  Integers are usually numbers.

I'm missing something here.

The Arduino docs say digitalRead returns HIGH or LOW. It is an integer value but is supposed to be restricted to those 2 values.

digitalRead, HIGH and LOW are defined in the Arduino header as

int digitalRead(uint8_t pin);

#define HIGH 0x1
#define LOW  0x0

The change is to not check for HIGH or LOW and that seems to work.

Say what?

digitalRead is not guaranteed to return HIGH or LOW?

 

 


The one who has the most fun, wins!


   
ReplyQuote
(@bits2bots)
Member
Joined: 1 year ago
Posts: 30
 

It does just that. My original code was in excess of what I needed so this suggestion was to simplify it to what is really required. I was trying to debug code that was working. Me being a hardware guy took reference to a pin to be physical pin. The compiler just disagreed.



   
ReplyQuote
(@bits2bots)
Member
Joined: 1 year ago
Posts: 30
 

Later I'll define the digitalRead variables as boolean.



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 444
 

@bits2bots 

Posted by: @bits2bots

It does just that. My original code was in excess of what I needed so this suggestion was to simplify it to what is really required. I was trying to debug code that was working. Me being a hardware guy took reference to a pin to be physical pin. The compiler just disagreed.

I'm not understanding this at all.

By "it" do you mean digitalRead or your code?

Let me check my premise: digitalRead returns an integer that is restricted to either HIGH or LOW.

The code from github for digitalRead (return values highlighted):

int digitalRead(uint8_t pin)
{
	uint8_t timer = digitalPinToTimer(pin);
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);

	if (port == NOT_A_PIN) return LOW;

	// If the pin that support PWM output, we need to turn it off
	// before getting a digital reading.
	if (timer != NOT_ON_TIMER) turnOffPWM(timer);

	if (*portInputRegister(port) & bit) return HIGH;
	return LOW;
}

So digitalRead does indeed return only 2 integer values. There's an implicit conversion of the constants for HIGH and LOW to integer.

The original, non-working code was

const int inPin = 10;
int pinState = 0;

void setup() {

    //...
	pinMode(inPin, INPUT_PULLUP); //use internal pullup
}
void loop() {
	pinState = digitalRead(inPin);
	if (pinState == HIGH)
	{
		Serial.println("1");
	}
	else {
		Serial.println("0");
	}
	delay(1000);
}

The changed, now working code is

    //...
void loop() {
	pinState = digitalRead(inPin); // reading D10 not pin 10
	Serial.println(pinState);
	delay(2000);
}

So, by not checking the return value of  digitalRead, the circuit worked?

Could you post the output from this program?


The one who has the most fun, wins!


   
ReplyQuote
(@bits2bots)
Member
Joined: 1 year ago
Posts: 30
 

Both ways if coding work. The code is fine. Never was broke. I was toggling the wrong input pin low and high on the Nano. I'd post a video but 10mb limit...



   
ReplyQuote
TFMcCarthy
(@tfmccarthy)
Member
Joined: 2 years ago
Posts: 444
 

@bits2bots

Posted by: @bits2bots

Both ways if coding work. The code is fine. Never was broke. I was toggling the wrong input pin low and high on the Nano. I'd post a video but 10mb limit...

Ah! A circuit error. OK, that adds up. I was worried I was losing my mind.


The one who has the most fun, wins!


   
ReplyQuote
(@bits2bots)
Member
Joined: 1 year ago
Posts: 30
 

So it's been 30 years since I wrote any code and even then not much before my career path took me away. I ended up heading up our electronics hardware design and engineering teams. Then I nixed all of that for a career in medicine. So I'm back retired and at where it all began. Tons of Arduino videos later I'm designing a universal transceiver controller to adapt to any crystal controlled radio for frequency agility. No CB radios, but commercial two way stuff that is sitting who knows where. I have a few in my stash so overfilling the effort to save a few. Perhaps others may be inspired when I'm done.



   
ReplyQuote
Page 3 / 3