Go Down

Topic: Arduino Tiny (Read 64 times) previous topic - next topic

spycatcher2k

((reading / 1024) * 5);

reading is 0 ->1024
therefor reading/1024 will be 1 or less (Probably rounded to 1) * 5 = 5,
what value range are you trying to get?
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Erni


lightningserpent

Thanks for the link to the tmp36 code. I originally used that code and from project to project it has evolved. My issues, for a reason I am not too sure of, stemmed from not using "float". I toyed with code straight from adafruit's page and found that it broke if I did not float my values. Thank you for your help. I am finished with the coding. Because the "arduino tiny" core was so easy to implement I no longer need to embed 328's for simple projects like this. One final question:

Is there an easy way to use the ATtiny85's internal temperature sensor through ArduinoISP?

Here is the current completed code (when I embed this I will remove the "serial" functions).
Code: [Select]

const int r = 4;//red
const int b = 0;//blue
const int g = 1;//green
const int s = 1;//sensor *ANALOG

int temp, currentTemp;
int previousTemp=0;

long previousMillisTIME= 0;
unsigned long intervalTIME = 1000;

void setup(){
  Serial.begin(9600);
  pinMode(r, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b, OUTPUT);
}

void loop(){

  unsigned long currentMillisTIME = millis(); 
  if(currentMillisTIME - previousMillisTIME > intervalTIME) { 
    sense();
    previousMillisTIME = currentMillisTIME;
  }       

  int flick = random(120)+135;//candle like effect

  if (temp < 68){//only show blue
    digitalWrite(r, LOW);
    digitalWrite(g, LOW);
    analogWrite(b, flick);
  }
  else {
    if (temp > 72){//only show red
      digitalWrite(b, LOW);
      digitalWrite(g, LOW);
      analogWrite(r, flick);
    }
    else {//only show green
      digitalWrite(b, LOW);
      digitalWrite(r, LOW);
      analogWrite(g, flick);
    }
  }
  delay(random(150));
}

void sense(){
  delay(10);
  int reading0 = analogRead(s);
  delay(10);
  int reading1 = analogRead(s);
  delay(10);
  int reading2 = analogRead(s);
  delay(10);
  int reading = ((reading0+reading1+reading2)/3);//average readings
  float voltage = reading * 3.3;//voltage reference
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  currentTemp = temperatureF;

  if(currentTemp - previousTemp >= 3) {//hysteresis forward
    temp = temperatureF;
    Serial.print("Changed From ");
    Serial.print(previousTemp);
    Serial.print(" to ");
    Serial.println(currentTemp);
    previousTemp = currentTemp;
  }
  else {
    if(currentTemp - previousTemp <= -3) {//hysteresis backward
      temp = temperatureF;
      Serial.print("Changed From ");
      Serial.print(previousTemp);
      Serial.print(" to ");
      Serial.println(currentTemp);
      previousTemp = currentTemp;
    }
  }
    //debugging
  Serial.print(voltage);
  Serial.println(" volts");
  Serial.print(temperatureC);
  Serial.println(" degrees C");
  Serial.print(temperatureF);
  Serial.println(" degrees F");
  Serial.print("current reading ");
  Serial.println(temp);
  Serial.println(" ");
}

Dr_Ugi

#198
Jun 23, 2012, 10:31 am Last Edit: Jun 23, 2012, 10:39 am by Dr_Ugi Reason: 1
I'm trying to use the ATtiny85 Arduino Tiny core in a project where I need to use the 2.56v internal reference for the ADC - not least because I need the ARef pin for other things, but also so that the reading is not directly dependent upon VCC, which in a battery powered situation is not very uniform. I appreciate that we need to use the non-decoupled 2.56v to avoid connecting pin 5 to that voltage.

I have tried poking at ADMUX without success and wondered:

Does analogRead always use the external Vref?
If so, can I read ADCL & ADCH directly?  I have tried but seem to be getting zeros.

Any thoughts appreciated.

Edit - it appears that by poking ADMUX I can alter the reference voltage (I can set it so that the 2.5v appears at PB0) but as soon as I call AnalogRead it reverts to the external ref.

Coding Badly


Huh.  Looks like the coin flip had me pick the wrong one to include...
http://code.google.com/p/arduino-tiny/source/browse/trunk/hardware/tiny/cores/tiny/wiring.h#105
[font=Courier New]
  analogReference( 6 );[/font]

...should do the trick.  Do you have a preference for the name of the constant (other than [font=Courier New]INTERNAL2V56[/font])?

Go Up