Pages: [1]   Go Down
Author Topic: analogRead conflicting with digitalwrite  (Read 1061 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Has anyone run into this problem before? I am using almost all the I/O pins as digital outputs to drive an LCD display directly. As a demo I have a counter going from 00 to 99 and it works fine. I am using digital pins 0-17 except for the crystal pins. I have to use three of the analog pins as digital outputs.

When I try to read from analog pin 6, one of the segments (tied to d2) goes blank and does not switch! I cannot figure this one out! Below is the code. I know it's not pretty but I am just starting out with this stuff.

My biggest problem is I don't know if this is a hardware or software bug. My next step is to use the AVR ISP programmer to try and load the bootloader into another 168 and see if it does the same thing. Lots of learning to do.

BTW, this is a boarduino.
Thanks for any help!
Moby

Code:
//  LCD pin map
//
//    -------        ---8---         ---1---
//                 13|     |9       6|     |2
//                   |     |         |     |
//                   --14---         ---7---
//                 12|     |10      5|     |3
//                   |     |         |     |
//    -------        --11---         ---4---
// ASCII to LCD element map
unsigned char LCD_ascii_to_elements[] =
{
    0x7e,     // '0'
    0x0c,     // '1'
    0xb6,     // '2
    0x9e,     // '3
    0xcc,     // '4
    0xda,     // '5
    0xfa,     // '6
    0x0e,     // '7
    0xfe,     // '8
    0xce      // '9'
};

// LCD element to chip pin map
unsigned int LCD_elements_to_pins[] =
{        //element
    2,   //1
    1,   //2
    15,  //3
    14,  //4
    16,  //5
    3,   //6
    4,   //7
    12,  //8
    5,   //9
    8,   //10
    9,   //11
    10,  //12
    6,   //13
    7    //14
};

int i;
int j;
int k;
int digit;
int del;
int temp;
unsigned char nibble_lo;
unsigned char nibble_hi;
unsigned int mask;
unsigned char ones;
unsigned char tens;

void setup()             // run once, when the sketch starts
{
  for(i=0;i<=17;++i){
    pinMode(i, OUTPUT);  // sets the digital pin as output
  }
  
    pinMode(PC6, INPUT); // sets the digital pin as output
}

void loop()              // run over and over again
{
   j=0;
   del=10000;
   digit=0;
   for(;;){
      ++j;
      if(j>=15){
         j=0;
         temp = analogRead(PC6);
       temp = temp/10;
       ++digit;
       if(digit>99){digit=0;}
      } else {
       ones=0;
       tens=0;
       for(k=0;k<=digit;++k){
         ++ones;
         if(ones>9){
            ones=0;
            ++tens;
         }
         if (tens>9) tens = 0;
       }
         nibble_lo = LCD_ascii_to_elements[ones];
         nibble_hi = LCD_ascii_to_elements[tens];
         digitalWrite(0, LOW);      // sets the common low
       mask = 0x02;
       for(k=0;k<=6;++k){         // set the one bit on
           if(nibble_lo & mask){
             digitalWrite(LCD_elements_to_pins[k], HIGH); // sets the LCD on
         } else {
             digitalWrite(LCD_elements_to_pins[k], LOW); // sets the LCD off
         }
         mask <<= 1;
       }
       mask = 0x02;
       for(k=7;k<=14;++k){        // set the one bit on
           if(nibble_hi & mask){
             digitalWrite(LCD_elements_to_pins[k], HIGH); // sets the LCD on
         } else {
             digitalWrite(LCD_elements_to_pins[k], LOW); // sets the LCD off
         }
         mask <<= 1;
       }
         delayMicroseconds(del);     // waits for a second
         digitalWrite(0, HIGH);     // sets the common low
       mask = 0x02;
       for(k=0;k<=6;++k){        // set the one bit on
           if(nibble_lo & mask){
             digitalWrite(LCD_elements_to_pins[k], LOW); // sets the LCD on
         } else {
             digitalWrite(LCD_elements_to_pins[k], HIGH); // sets the LCD off
         }
         mask <<= 1;
       }
       mask = 0x02;
       for(k=7;k<=14;++k){        // set the one bit on
           if(nibble_hi & mask){
             digitalWrite(LCD_elements_to_pins[k], LOW); // sets the LCD on
         } else {
             digitalWrite(LCD_elements_to_pins[k], HIGH); // sets the LCD off
         }
         mask <<= 1;
       }
         delayMicroseconds(del);                   // waits for a second
      }
   }
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, previous code should read analogRead(5);

So, I swap digital output pins with another pin and it works. I hook up an led to digital pin 1 and just drive it low. If I do the analogRead, the LED flickers. If I comment out the analogRead, the LED stays low.

SOmething is causing my analogRead on analog input 5 to cause the digital output on digital pin 1 not to work! If I don't do the analogRead, everything works as expected. Why is the analogRead dorking up my digital output 1? smiley-sad

Thanks,
Moby
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Did you also adjust the use of PC6 in setup()?

Are you aware that digital I/O pin 1 is also the USB serial port's transmit pin?  I don't see any Serial.print() but that would definitely hork thinks up.

To explicitly set an analog pin as input, you have to say pinMode(<myanalognumber> + 14, INPUT);  (Since pinMode() expects the numbers 0 to 19, not the analog numbering scheme.)  By default, everything's an input, which is why you often don't see this wrinkle in example sketches.  What you have does no harm to the input, but now your 5th LCD-out pin (digital pin 5) isn't going to work as an output.

So in conclusion, are you sure it's output 1 that's horked, and not output 5?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for replying!

Yes, I know I'm sharing pins with the serial I/O because I need 16 outputs to drive the LCD. I have used the serial.print to display the analog value that I measured and it measures correctly so I know I am reading the correct pin.

When I comment out my analogread, digital pin 1 works correctly. If I read analog pin 5, digital pin1 does not work correctly. It looks like it is switching but not the way I am programming it.

I wish I had another 168 to try it out on but I only have 88's and I am trying to get my program to run on that but it does not have a bootloader on it so I have an AVR ISP programmer that I am trying to get working.

I am all very new to this although I have a lot of IC design experience and understand how micro controllers work, I just need to learn the programming environment.

Thanks Again,
Moby
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, got my atmega88 bootloader going! Many thanks to

http://wiki.edwindertien.nl/doku.php?id=boards:cheapduino

I loaded my code and it does exactly the same thing! Is there some connection between using some of the analog pins as digital pins and just one analog pin as a A/D input and messing up digital pin 1? All the other digital I/O's work OK as well as the three analog pins I'm using (digital pins 14-17).

I am stumped! Is it a compiler problem or some of the library code? Time to start digging deeper I guess. Well, at least I learned how to use my ISP programmer and how to load a bootloader!

Moby
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, more investigation reveals that digital pin 0 and digital pin 1 are switching identically.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Woot! It was my brain fart! I had an index accessing my array of pins that was going one over the size of the index (starts at 0 not 1). So what ever was in the memory location following the array it was reading as a pin and writing to it. For some reason the addition of the analog read changed that memory.

Note to self: Be more careful with your code!

Thanks for all your help!

Moby
Logged

Pages: [1]   Go Up
Jump to: