Code on ATTiny85 not running properly (compared to UNO)

Hi All

I’m have a problem with the my UNO code running on a ATTiny85.

I’ve run the code on a Uno without problem, but after loading it on a ATTiny85 it doesn’t work as it should.

The code basically compares one analog (sensor) vs another (threshold setting), and behaves in a particular way depending on the analog value. I’ve run the code on my Uno and it works fine, however after loading it onto a ATTiny85, it tends to bypass one of the If else statements.

The code does run on the Attiny85, as the LED flashes the modes number (as per code) except mode 2, which just gets bypassed! As the analog voltage is increased on the sensor analog pin, it jumps from mode 1 directly to mode 3 then on to mode 4! however when I use the same code on my UNO it works properly! mode 1 - 2 - 3 - 4.

I’m using the ATTINY85 files from David A Mellis (GitHub - damellis/attiny: ATtiny microcontroller support for the Arduino IDE)
IDE 1.0.1 as requested by above software.
Tested on 3.3V and 5V, not difference

const int sensor = 3 ;  // A0 On UNO
const int threshold = 4 ;  // Use A1 on UNO

int shorttoground = 50; //Declare short to ground value
int shorttopositive = 975; //Declare short to positive value

int sensorValue = 0;        // value read from the sensor (pot for testing purposes)
int thresholdValue = 0;    // value read from the pot 
int led = 1;                            // use 13 on UNO
int relay = 0;                      // use 12 on UNO

void setup() {
  // initialize serial communications at 9600 bps:
//  Serial.begin(9600); 
  pinMode(led, OUTPUT);
  pinMode(relay, OUTPUT);
}

void loop() {
  // read the analog in values:
  sensorValue = analogRead(sensor);  
  thresholdValue = analogRead(threshold);
  
 // Serial.print("sensor :");
 // Serial.print(sensorValue);
 // Serial.print("   Threshold :");
 // Serial.println(thresholdValue);  
  

  if  (sensorValue < shorttoground) // mode 1
  {
    digitalWrite(relay, LOW);
    
      digitalWrite(led, HIGH);
      delay(600);
      digitalWrite(led, LOW);
      
  }
   
//////// This is the one being bypassed!    ////////////

    else if (sensorValue >= shorttoground && sensorValue < thresholdValue) // mode 2
    {  
    digitalWrite(relay, LOW);
    
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
      delay(200);
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
  }
  
  
    else if (sensorValue >= thresholdValue && sensorValue < shorttopositive) // mode 3
     {
      
     digitalWrite(relay, HIGH);
     
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
      delay(200);
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
      delay(200);
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
     }
   
   
     else if (sensorValue >= shorttopositive) //mode 4 
     {
      digitalWrite(relay, LOW);
      
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
      delay(200);
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
      delay(200);
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
      delay(200);
      digitalWrite(led, HIGH);
      delay(200);
      digitalWrite(led, LOW);
     }
      
  delay(500);  
}

Thanks in advance!

If the code works properly on your Uno it is reasonable to assume the logic is working the same on the Attiny. In that case it is the values detected that are causing the problem. In other words it is "correctly" skipping mode 2.

Have you a means to print debug data to the serial monitor from the Attiny - if so, put in some statements so you can see what is the value seen by the ADC.

Are you sure you are referring to the correct pins on the Attiny? I've used an Attiny 45 but I can't remember what I had to change, and I wasn't using the ADC.

And please write a function to flash the led the desired number of times using a "for" loop and dispense with all those lines with delays.

...R

Hi Robin2

Thanks for your reply!

I don't have any means of debugging, perhaps I could connect an I2C LCD to check values?

Im pretty certain the pinout is correct, as if I chance the voltage on the sensor pin it acts accordingly, except the mode 2 part! if I short the analog pin to ground I it flashes mode 1, if I short to Vcc I get mode 4. to the function is correct! however it just seams to miss mode 2 Grrr

I just don't understand what's wrong!

else if (sensorValue >= shorttoground && sensorValue < thresholdValue) // mode 2
else if (sensorValue >= thresholdValue && sensorValue < shorttopositive) // mode 3

If threshholdValue is less than shorttoground mode 2 will be skipped. You could change your code to be valid for all pot readings by

int temp =(thresholdValue,0,1023,shorttogorund,shorttopositive);
thresholdValue =temp;

This would yield a thresholdValue between shorttoground and shorttopositive, unless the pot is returning zero.

Hi Hasseklas

Thanks for you reply but I'm a bit confused.

I'm not sure what you mean, as if the as if the sensor value is below that of what is defined (50 in this case) in the code, it should hold in "mode 1", as the value increases above 50 and below the (variable) threshold, lets just say 400 for now, then it should progress to mode 2, once the value increases above the threshold (400) and below 975 (defined again) progress to mode 3, anything above 975 will be mode 4.

0-49 = Mode 1 50 - Threshold-1 = Mode 2 threshold - 974 = Mode 3 975 - 1023 = Mode 4.

Sorry, not sure if I'm getting myself confused here.

I'm often confused 8) the thing is you let the pot decide the threshold. If that reading is bad , ie below 50, mode 2 will not be entered.

LOL, at least I'm not alone :D

I been fiddling around, at it appears there was a problem with the analog pin reading the threshold voltage.

So I changed the threshold to another analog pin (P2 from P4) and started getting somewhere, but still got problems.

The ATTiny85 is running off the 5V supplied from the Uno, and I'm running the same code on the Uno with the two analog pins of the Uno tied into the same pots (sensor and Threshold) so I can monitor the readings and confirm the operation. Once again the Uno runs 100% correct, but now the Attiny85 has picked up another problem.

0-50 - I get mode 1 on both the Uno and Attiny, after 50 they both go into mode 2, and the relay outputs active which is great! but at about 100 the Attiny jumps between mode 2 and 3., while the Uno continues to be stable all the way up to the correct threshold of mode 3.

If I continue to increase the analog voltage they both go into mode 4.

The symptoms you describe are consistent with the voltage measured by the ADC jumping from one level to another rather than increasing smoothly. I suspect the movement to the different modes is happening for some spurious reason which just appears to be correct.

There is a version of the Attiny code that supports some form of serial.print for debugging. You should find it if you search the forum. Then you can put some debugging commands on your Attiny. If you take the Atmega328 out of the Uno you can use it like an FTDI cable to connect your PC to the Attiny.

I recommend buying an FTDI cable as it's very useful for connecting to "arduino" chips that don't have USB connectors.

...R

Trying not to assume anything…

Which physical pins do you have connected for these?

const int sensor = 3 
const int threshold = 4

On ATtiny85:
Pin 1 == PB5 == Analog A0
Pin 7 == PB2 == Analog A1
Pin 2 == PB3 == Analog A3
Pin 3 == PB4 == Analog A2

When using analogRead(), Arduino compiler ‘assumes’ the pin number to be the “Analog A#”.

If you are connected to PB2 for threshold, I would write it as this-

const int threshold = A1;      // ADC1, PB2, physical Pin 7

Thanks for the replies!

Robin2, will look at getting an FTDI Cable.

1ChicagoDave, Thanks for the pointer, something i honestly overlooked, Although I had the correct numbers, I did not use an "A" to define analog, however as the sensor was functioning in a analog style, otherwise it would simple default to mode 1 or 4, but I will try amending it and testing it, ifit works I'm going to kick my .........

I've just pulled out an Arduino Pro Micro (Leonardo) and uploaded the code and it works perfectly, so who knows!

If you are running the attiny from a breadboard it could be that you have a bad connection for some pins.

1ChicagoDave, taking you advise, i tried to place an “A” in front of the number, but of course it would take it on A4 and through an error.
however after some playing around I found the following and managed to get it to work, but I’m still a bit confused :~

I used :

A3 for the sensor
A2 for the threshold

However A3 turns out to be physically Pin 7 on the IC - PB2 - analog1
and A2 is physically Pin 1 on the IC - PB5 - analog 0

which doesn’t tie up with the documentation I received with the programmer or the diagram posted above! could it be a problem with the library?