Go Down

Topic: How do I check the state of an output pin? (Read 1 time) previous topic - next topic


Let's say I've set an pin in OUTPUT mode either HIGH or LOW, and I don't want to have to keep track of what I've set it to myself, but I need to know later in my code what I've set it to so I don't try to turn it off over and over again.

Will a call to DigitalRead() return this state?  And is doing that fast?  IF not, is there some value in an array somewhere which I can read back the last state I set it to?

This is the code I'm having trouble with:
Code: [Select]

void updatearray() {

 int firstpin = 2;
 int dutycycle = 1785; // Dutycycle = 1000000 microseconds / 70hz update / groups.  1785 = 8 groups of 8 at 70hz.
 unsigned long delaystart;
 int timeelapsed;
 for (int x = 0; x < 64; x+=8) { // Simulate updating 64 leds.
   if (x < 8) {
     for (int y = 0; y < 8; y++) {
       if (array[x+y] > 0) {
         digitalWrite(firstpin + x + y, HIGH);

     delaystart = micros();
     do {

       timeelapsed = micros()-delaystart;
       for (int y = 0; y < 8; y++) {          
         // Divide the brightness value in the array by 255 to produce a number between 0 and 1.  Multiply dutycycle by this value to determine how long we should wait before turning the led off.
         if (timeelapsed > (dutycycle*(array[x+y] / 255.0))) {
           digitalWrite(firstpin + x + y, LOW);
     } while (timeelapsed < dutycycle); // Regardless of whether all LED's are off, wait for duty cycle to complete.
     for (int y = 0; y < 8; y++) {
       digitalWrite(firstpin + x + y, LOW);
   else {
     delayMicroseconds(dutycycle); // Removing this delay would make the leds brighter when fewer are lit, and could burn them out if we are pulsing them at higher than their rated current.

It's a little confusing because I'm simulating how bright an 8x8 array would be with just 8 leds, so it just delays after the first column is rendered.  But basically, the idea is that the values in my array should define brightness levels for each led, where a value of 255 should make the led light for the full 1785 microseconds, and a value of 1 would make it light for just 1 microsecond.  

The problem is, while the leds I've set to the lowest values are dimmer, they're only slightly dimmer.  A brightness of 1 isn't even CLOSE to being full off.  It's more like 50% off.  So I'm thinking maybe setting the ports off over and over again is a bad idea.


I think reading will work - unless digitalRead changes the pinMode which I doubt.

If you look at the code for digitalRead and read the ATmega datasheet you'll find the answer (though much simpler to just try it - a truly definitive answer!)
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


I did just try it, and I assume it's working, but I don't see any change in the brightness of my leds, so either it's always reading high, or reading is as slow as writing.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131