-1

My UNO having some stability issues because of heavy memory use, I'm now optimizing the code now looking to reduce the digital read memory use with the help of port manipulation. I got the code for 'digitial write' but no idea for the digital read pinMode(powerStatus, INPUT);digitalRead(powerStatus) Thanks

8
  • 2
    how can direct port manipulation instead of digitalWrite/Read reduce dynamic memory use? – Juraj Nov 28 '20 at 10:15
  • Did you read the Arduino tutorial on the subject? – Edgar Bonet Nov 28 '20 at 10:21
  • 5
    less code will free flash, but not SRAM. even 100% use of flash will not create stability issues – Juraj Nov 28 '20 at 11:20
  • 3
    "Heavy memory use" as you put it does NOT cause "stability issues". What EXACTLY do you mean by "stability issues" here? If you mean that the part doesn't run reliably and resets itself, it's more likely you have a power supply issue. If would really help if you would edit your question and indicate exactly what you are doing, what problem you are seeing, etc. General terms like "stability" and "heavy use" really mean nothing. – jwh20 Nov 28 '20 at 11:35
  • 1
    Stop using String in your code. That will cure 99% of all memory stability issues. – Majenko Nov 28 '20 at 11:56
1

To read a pin you simply read the port the pin is part of then mask out which pin within that port you want to read.

For example to read pin D11 you need to read bit 3 of port B.

if (PINB & (1 << 3)) { 
    // HIGH
} else {
    // LOW
}

However this doesn't address the underlying problems you're having with your sketch. Saving on flash memory by optimizing your code will not fix instabilities (if anything it will introduce new instabilities from new untested code).

Instabilities related to memory are due to a lack of (or fragmentation of) SRAM not flash. Optimizing code like this will save flash, but won't directly affect your SRAM usage at all.

Instead you need to optimize your SRAM usage by:

  • Make sure all string literals are kept in PROGMEM by using the F() macro where appropriate
  • Avoid use of String and instead stick to char * with pre-allocated buffers.
  • Don't use new and delete or malloc and free as this will fragment your heap and cause instability (this is the underlying problem with String).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.