Code works on Mega but not on attiny13 nor 85

I made this simple code to give me a visual feedback of air quality inside my workplace, and I want to use an ATtiny13a, but the LEDs simply light up and don't change as they're supposed to. I don't think it's a core issue because I've tried on two different versions of the micro core and I also tested on the ATtiny85. It only works on the arduino mega.

I have no idea what might be.

int HarmfulGases = 0;

void setup() {

  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  HarmfulGases = analogRead(A2);
  if ( HarmfulGases <= 70 ) {
    digitalWrite(0, HIGH);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
  }
  else if ( HarmfulGases > 70 && HarmfulGases <= 120 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
  }
  else if ( HarmfulGases > 120 && HarmfulGases <= 200 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
  }
  else if ( HarmfulGases > 200 && HarmfulGases <= 280 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
  }
  else if ( HarmfulGases > 280 && HarmfulGases <= 350 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, LOW) ;
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
  }
  else if ( HarmfulGases > 350 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
  }
}

Which board package do you use? Please provide a link to it. My guess is that A2 isn't correctly configured.

I don't know... It didn't work on the 85 either. But here's the core: https://mcudude.github.io/MicroCore/package_MCUdude_MicroCore_index.json

Which core do you use for the ATtiny 85? Do you use the default core_settings.h?

I use this one here: https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json but i need it to work on the ATtiny13a, the 85 was just a test...

HarmfulGases = analogRead(A2);

For the core you are using, which pin is A2?

Do you have your sensor connected to the correct pin?

It's physical pin number 3. Yes, I am using the right pin according to the datasheet... Is there a way to use the real AVR names? I've done that with digital pins like "pcint" but it doesn't work when I put ADC2 for example.

attiny85.png

Try 2 instead of A2. Maybe A2 is not correctly defined.

Whaaaat hahaahah, it's working!!!!!!! Thank you VEEERRRY much! I just changed A2 to 2.

int HarmfulGases = 0;

void setup() {

  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  HarmfulGases = analogRead(2);
  if ( HarmfulGases <= 70 ) {
    digitalWrite(0, HIGH);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
  }
  else if ( HarmfulGases > 70 && HarmfulGases <= 120 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
  }
  else if ( HarmfulGases > 120 && HarmfulGases <= 200 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
  }
  else if ( HarmfulGases > 200 && HarmfulGases <= 280 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
  }
  else if ( HarmfulGases > 280 && HarmfulGases <= 350 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, LOW) ;
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
  }
  else if ( HarmfulGases > 350 ) {
    digitalWrite(0, LOW);
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
  }
}

You are welcome. Glad to know it is working.