Go Down

Topic: analogRead on A1 won't work after porting code to attiny85 (Read 216 times) previous topic - next topic

Henrikmon

Hi.
I'm having problems porting my code from a arduino to an attiny85.
I've changes the pin numbers to match the layput of attiny85, but for som reason the A1 analogRead is not working correctly. I am pretty sure I have some sort of error in my code that I don't see, so hopefully you Guys can help me out.
My code is as follow:
Code: [Select]

//Declaring variables
//Pin assignment
                         //Reset            - Pin 1
byte inPin = 3;          //Digital input 3  - Pin 2
byte analogFade = 2;     //Analog input 2   - Pin 3
                         //Ground           - Pin 4
byte outPin = 0;         //PWM output 0     - Pin 5
byte dimPin = 1;         //Digital input 1  - Pin 6
byte analogDim = 1;      //Analog input 1   - Pin 7
                         //+5V power        - Pin 8


//Variables used
bool onOffSwitch = 0;    //Current state of on/off input
int fadeValue = 0;       //Current fede value
int delayIn = 0;         //Delay in fadein cyclus
int delayOut = 0;        //Delay in fadeout cyclus
bool dimSwitch = 0;      //Current state of dimmed/on input
byte dimSetting = 255;   //See if set to dimmed or full on
byte dimLevel = 255;     //Currrent level of dim



//Functions
//Check if input is set to DIM or ON
void dimCheck() {
  //Read input
  dimSwitch = digitalRead(dimPin);
  dimLevel = analogRead(analogDim) / 4;
 
  //Check whether DIM input is on...
  if(dimSwitch == LOW) {
    dimSetting = 255;
  }
 
  //...or off
  if(dimSwitch == HIGH) {
    dimSetting = dimLevel;
  }
}


//Fade light in
void fadeIn() {
  //Run over and over till fadeValue hits dimSettings
  for(fadeValue = fadeValue; fadeValue < dimSetting; fadeValue++) {
    //Read inputs
    onOffSwitch = digitalRead(inPin);
    delayIn = analogRead(analogFade) / 8;
   
    //Make check of dim input
    dimCheck();
   
    //Exit loop if switched off
    if(onOffSwitch == HIGH) {
      break;
    }
   
    //Exit if target is hited
    if(fadeValue > dimSetting) {
      break;
    }
   
    //Write to analog output
    analogWrite(outPin, fadeValue);
   
    //Wait before running loop again
    delay(delayIn);
  }
}


//Fade light out
void fadeOut() {
  //Run over and over til fadeValue hits dimSettings
  for(fadeValue = fadeValue; fadeValue > 0; fadeValue--) {
    //Read inputs
    onOffSwitch = digitalRead(inPin);
    delayOut = analogRead(analogFade) / 8;
   
    //Make check of dim input
    dimCheck();
   
    //Exit loop if switched on
    if(onOffSwitch == LOW) {
      break;
    }
   
    //Write to analog output
    analogWrite(outPin, fadeValue);
   
    //Wait before running loop again
    delay(delayOut);
  }
}


//Fade light down
void fadeDown() {
  //Run over and over til fadeValue hits dimSettings
  for(fadeValue = fadeValue; fadeValue > dimSetting; fadeValue--) {
    //Read inputs
    onOffSwitch = digitalRead(inPin);
    delayOut = analogRead(analogFade) / 8;
   
    //Make check of dim input
    dimCheck();
   
    //Exit loop if switched off
    if(onOffSwitch == HIGH) {
      break;
    }
   
    //Exit loop if dim level is higher than current light level
    if(fadeValue < dimSetting) {
      break;
    }
   
    //Write to analog output
    analogWrite(outPin, fadeValue);
   
    //Wait before running loop again
    delay(delayOut);
  }
}


//Setup after reset
void setup() {
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT_PULLUP);
  pinMode(dimPin, INPUT_PULLUP);
}

//The loop routine runs over and over again forever:
void loop() {
  //Read inputs
  onOffSwitch = digitalRead(inPin);
  dimLevel = analogRead(analogDim) / 4;
 
  //Make check of dim input
  dimCheck();
 
  //Check if switched on and light level lower than target
  if(onOffSwitch == LOW && fadeValue < dimSetting) {
    fadeIn();
  }
 
  //Check if switched off and light level higher than off
  if(onOffSwitch == HIGH && fadeValue > 0) {
    fadeOut();
  }
 
  //Check if light level is higher than target
  if(fadeValue > dimSetting) {
    fadeDown();
  }
 
  //Prevents program to enter loops, if target is reached
  if(fadeValue == 0) {
    digitalWrite(outPin, LOW);
  }
 
  //Prevents program to enter loops, if target is reached
  if(fadeValue == 255 && dimSetting == 255) {
    digitalWrite(outPin, HIGH);
  }
 
  //Prevents program to enter loops, if target is reached
  if(fadeValue == dimLevel && dimSetting == dimLevel) {
    analogWrite(outPin, dimLevel);
  }
 
  //Delay for program stability
  delay(100); 
}

DrAzzy

I don't see anything outwardly wrong with that code, and the pins look right.

What tiny85 core are you using? Have you verified that the pinmapping (for things other than analogRead) is what you think it is?

I have seen two pin mappings for the tiny84 that numbered the pins in opposite orders; I think the same one that did it differently for the 84 did so for the 85 too.
Tiny841 + 1634 Core:http://drazzy.com/e/tiny841.shtml
ATTiny core for 1.6.x, for x4/x5/x61/x7/x8 series) https://github.com/SpenceKonde/ATTinyCore

Henrikmon


DrAzzy

Tiny841 + 1634 Core:http://drazzy.com/e/tiny841.shtml
ATTiny core for 1.6.x, for x4/x5/x61/x7/x8 series) https://github.com/SpenceKonde/ATTinyCore

Henrikmon

I can adjust the fadeIn and fadeOut speed fine with a trimmer on A2 and the fading in and out works fine, but no matter the voltage state of A1 the output go to +5V. I have tried diffrent stages of the digital dim pin too (dimPin), to see if the error was there.

Runaway Pancake

What if you change
byte analogDim = 1;
to
byte analogDim = A1;
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"
Hey, it's "bipolar transistor" or "junction transistor" - "BJT" is just stupid.

Henrikmon

In the code uploaded here it is changed from A1 to 1 due to the same error. The error stands with both A1 and 1. The other analog reading works with both Á2 and 2.

DrAzzy

Shouldn't make a difference.


Code: [Select]

static const uint8_t A0 = 6;
static const uint8_t A1 = 7;

(snip)

#define analogPinToChannel(p)   ( (p) < 6 ? (p) : (p) - 6 )



One thing that would be interesting is to use software serial (or writing to EEPROM) to record the value it's analogRead()ing.
Tiny841 + 1634 Core:http://drazzy.com/e/tiny841.shtml
ATTiny core for 1.6.x, for x4/x5/x61/x7/x8 series) https://github.com/SpenceKonde/ATTinyCore

Runaway Pancake

#8
Mar 21, 2015, 02:58 am Last Edit: Mar 21, 2015, 03:01 am by Runaway Pancake
...and verifying that physical pin 5 (D0) has PWM output at all (with a simple "test the PWM output works" sketch.)

> > > because you said that this is a working Arduino sketch, only the pin numbers have been changed.
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"
Hey, it's "bipolar transistor" or "junction transistor" - "BJT" is just stupid.

DrAzzy

Good call on testing that pwm actually works on that pin!

It *should*, and it does with the core I use - but the core I use does everything itself, rather than trusting the builtin core and just having entries for pins_arduino.h.

I think the core I used was this one:
https://github.com/SpenceKonde/arduino-tiny/tree/attiny1634
Tiny841 + 1634 Core:http://drazzy.com/e/tiny841.shtml
ATTiny core for 1.6.x, for x4/x5/x61/x7/x8 series) https://github.com/SpenceKonde/ATTinyCore

Henrikmon

The PWM output works fine. I've put an LED on the output and it fades just as it should. I can also adjust the speed with the trimmer given a analog signal on A2. The only thing that doesn't work is the dim setting. The LED fades to full on no matter what but it fade to that value.
Later today I will try to test the pin with a serialmonitor some way. I've also found another core, to see if that makes a diffrence.

Thank you for all your replys so far. It's nice to have some help figuring this out.

Henrikmon

Ok. I've tried with a much simpler code to test the analog input. The code i've tested is as follow:
Code: [Select]
void setup() {
  pinMode(0, OUTPUT);
}

void loop() {
  int val = analogRead(2);
  if(val <= 511) {
    digitalWrite(0, HIGH);
    delay(50);
    digitalWrite(0, LOW);
    delay(1000);
  } else {
    digitalWrite(0, HIGH);
    delay(50);
    digitalWrite(0, LOW);
    delay(100);   
    digitalWrite(0, HIGH);
    delay(50);
    digitalWrite(0, LOW);
    delay(1000);
  }   
}


Everything works. I will, when I got time, review my entire code to se if I've messed up somthing i my code. I will post my findings when i got them.

Henrikmon

After a long time I found that my problem was with the SRAM being full. After rewriting the code whit this in mind a found this solution:

Code: [Select]

//Pin konfiguration
int In0  = 3;   // On/Off kontakt
int In1  = 1;   // Dim kontakt
int Out0 = 0;   // LED output
int Ana0 = A1;  // Fade hastighed
int Ana1 = A2;  // Dim niveau


//Variabler der er i brug
int LightLevel = 0;
int LookupValue = 0;
int DimLevel = 16;
int OnOff = 1;
int Dim = 1;
int Speed = 0;


//Lookup Tabel
const int Lookup[] = {0, 1, 2, 3, 4, 6, 8, 12, 16, 23, 32, 45, 64, 90, 128, 180, 255};


//Funktioner
void ReadInputs() {
  OnOff = digitalRead(In0); 
  Dim = digitalRead(In1);
  Speed = analogRead(Ana0) / 4;
  DimLevel = analogRead(Ana1) / 64 + 1;
 
  if(Dim == 0) {
    DimLevel = 16;
  }
}


void FadeIn() {
  for (LookupValue = LookupValue; LookupValue <= DimLevel; LookupValue++) {
    ReadInputs();
   
    if(OnOff == 1) {
      break;
    }
   
    LightLevel = Lookup[LookupValue];
    analogWrite(Out0, LightLevel);
   
    delay(Speed);
  }
 
  if (LookupValue > DimLevel) {
    LookupValue = DimLevel;
  }
}


void FadeDown() {
  for (LookupValue = LookupValue; LookupValue >= DimLevel; LookupValue--) {
    ReadInputs();
   
    if(OnOff == 1) {
      break;
    }
   
    LightLevel = Lookup[LookupValue];
    analogWrite(Out0, LightLevel);
   
    delay(Speed);
  }
 
  if (LookupValue < DimLevel) {
    LookupValue = LookupValue + 1;
  }
}


void FadeOut() {
  for (LookupValue = LookupValue; LookupValue >= 0; LookupValue--) {
    ReadInputs();
   
    if(OnOff == 0) {
      break;
    }
   
    LightLevel = Lookup[LookupValue];
    analogWrite(Out0, LightLevel);
   
    delay(Speed);
  }
 
  if (LookupValue < 0) {
    LookupValue = 0;
  }
}

void setup() {
  pinMode(In0, INPUT_PULLUP);
  pinMode(In1, INPUT_PULLUP);
  pinMode(Out0, OUTPUT);
}

void loop() {
  ReadInputs();

  //Fade ind
  if (OnOff == 0 && LookupValue < DimLevel) {
    FadeIn();
  }
 
  //Fade ned
  if (OnOff == 0 && LookupValue > DimLevel) {
    FadeDown();
  }
 
  //Fade ud
  if (OnOff == 1 && LookupValue > 0) {
    FadeOut();
  }
   
  delay(250);
 
}

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!

Arduino
via Egeo 16
Torino, 10131
Italy