ATTiny85 + Arduino ISP + AnalogRead problem - Pin4

I've got a strange problem with an ATTiny85. Have a circuit setup that is using 2 resistive touch sensors to feed 2 inputs that turn on 2 LEDs. In short, touch sensor 1 > LED 1 goes on, touch sensor 2 > LED 2 goes on. I have confirmed that this works on an Arduino Uno. My setup has the 2 touch circuits feeding the ATTiny on Pin 3 and Pin 4 (the 2 analog inputs - see pic below). Everything works fine for Pin 3, but on Pin 4 the LED blinks on and off. I also swapped the touch sensor circuits to make sure the problem was with the ATTiny and not the electronics, and confirmed that it is the input on Pin 4 that seems to be failing.

I decided to strip things down and focus on Pin 4. I also stripped the code down to focus on Pin 4. The touch sensor works by supplying ~4.5V to Pin4 when running and not touched (so its HIGH). It is a resistive touch sensor, so when I complete the loop (by touching 2 pins with my finger), the circuit triggers a voltage drop. Using AnalogRead, this translates to the code seeing 1023 normally, and the value drops somewhere below 400 when touching the sensor. Again, this works for Pin3 and the code around Pin3. The code for Pin4 is identical (see below). When I run the code, it uses tchValB = analogRead(tchPadB); to get the voltage value, and the LED will go on and off constantly. I was able to confirm that the value is somewhere around 4 even without my touching the sensor. I did this by simply playing with *if (tchValB < tchValSens) * and setting tchValSens to lower and lower values until the LED stopped blinking. A value of 5 blinks, and 4 does not. Again, if I put the circuit input pin on Pin 3 it works fine and stays at 1023 until I touch it (so I know the circuit is fine), and it works on the Uno as well - both touch circuits read 1023 until touched. It seems that Pin4 is not reading/translating AnalogRead correctly. I used the HLT wiki example to get the Arduino IDE working with the ATTiny85. Has anyone heard of there being a problem with AnalogRead for this? I found something vague about another code porting example to make the Arduino IDE work with another ATTiny, and it mentioned an analogRead issue, but nothing specific to this one. Oh, I also tried another ATTiny chip as well to make sure that mine was not fried. Same result.

I'm stumped.

 //***********************
 //CONFIGURABLE VALUES
  int tchValSens = 990;                      //val 1023 is open circuit.  0 is closed.  Dry fingers use 900, humid, damp, use 100
 //************************
 
 #define statusLED 2                        //Digital In D1 - used to show timer countdown.
 #define ledCtrlrB 1                //Digital In D6  MUST BE A PWM PIN (oddly my PWM pin#11 does not work) - controls IRF530 transistor > LED lights
 int tchPadB = 4;                            //Analog In A2 Pin4

 int i = 0;
 int dlyTime = 20;                            //delay timer (for overall loop) in MS.
 int statCounter = 0;                         //counter for the status LED.
 int tchValB = 0;                            //Holder for tchValB
 
 int ledStateB = LOW;                          //Holds the current state of the LED lights (HIGH=on LOW=off)
 int startBrightness = 255;                   //Initial brightness of the LED  0-255
 int statusLEDFlashDur = 100;                 //In Milliseconds - Flash period for MSTimer2 to flash the LED in ms.  100ms

 void setup ()  
 {
  pinMode(tchPadB, INPUT);                   //Setup the tchPadB as an Input    
  pinMode(statusLED, OUTPUT);                 //Setup the statusLED as an Output
  pinMode(ledCtrlrB, OUTPUT);             //Setup the ledCtrlrB as an Output
 }


void flash() {
  digitalWrite(statusLED, HIGH);
  delay(60);
  digitalWrite(statusLED, LOW);
}

void rampLEDDownNoff(byte ledPin, byte lval, int dly, byte ledStateWhich)
{
  if (dly == 0)
  {
    analogWrite(ledPin,0);
  }
  else
  {
    while(lval > 0)
        {
          analogWrite(ledPin,lval);
          lval--;
          delay(dly);
         }
        //make it is all the way off
        digitalWrite(ledPin, LOW);
  }
 
    ledStateB = LOW;
}

void rampLEDupOn(byte ledPin, byte lval, byte ledNormVal, int dly, byte ledStateWhich)
{  
  if (dly == 0)
  {
    analogWrite(ledPin,255);
  }
  else
  {
    while(lval < ledNormVal)
        {
          analogWrite(ledPin,lval);
          lval++;
          delay(dly);
        }
  }
    ledStateB = HIGH;
}

 void loop (){
   


  tchValB = analogRead(tchPadB);

   

   if (tchValB < tchValSens)      
   {
       //check state and turn led on or off -  if the LED is off turn it on and vice-versa:
       if (ledStateB == LOW)
         {
          rampLEDupOn(ledCtrlrB, 0, 255, 0, 2); //5
         }
       else
         {
          rampLEDDownNoff(ledCtrlrB, 255, 0, 2);  //10
         }
         delay(500);
      }   
 }

Attiny85 pin 4 is analog pin 2, so you need "analogRead(2)" to read from that pin.

@Bobnova, thanks for the tip.. I just figured this out as well. After figuring this out, I found something else that is odd. My complete circuit uses all 3 analog inputs. Analog input 3 and 2 are being used for each touch controller - this is working now. When I touch each one, the related LED for each will toggle. However, when I enable the code for the status LED (which uses analog input 1 on the ATTiny85 - physical pin 7 - PB2), it causes the LED on Pin0 to flash instead of its own LED. It is almost like it is reassigning the port?

Problem solved.. :astonished:

I was looking at the HLT wiki article showing physical pin7 as a analog input 1, so this is the way I was trying to set it up. When checking the Atmel datasheet, it shows as PB2 (after PB0, PB1), so I assume this is a digital pin, but without the PWM. After setting them as shown below, it all works. Shesh... 4 hours of troubleshooting... Everyone can call off the search team... It was just me.. LOL....

#define statusLED PB2 //Digital Pin 2
#define ledCtrlrA PB0 //Digital Pin 1
#define ledCtrlrB PB1 //Digital Pin 0
int tchPadA = 3; //defined as Pin 3 (analog input 3) - physical pin 2 on ATT85
int tchPadB = 2; //defined as Pin 4 (analog input 2) - physical pin 3 on ATT85

Awesome, glad you got it figured out!