305Views6Replies

Author Options:

Can't properly flash Attiny85, please help. Answered

I have working proto of a bathroom exhaust fan controlled by methane sensor via arduino uno. I now wish to transfer this sketch to an ATtiny85 (cause i had extras). I have programmed ATtiny before, and can successfully get 'blink' going on this specific MCU. However I cannot get my sensor sketch to work properly... Perhaps I am asking too much of this little guy? I am including my code and video in hopes that someone can steer me in the right direction. 

/*
  AT85      Arduino    Circuit
  0 (PB5)   A0             pin 3 of 50k pot
  2 (PB7)   ~9             led or relay
 
  Trim Pot     
  1              GND
  2              GND
  3              Arduino (attiny) & Sensor B1
 
  Sensor
  A1             +5VDC
  H1             GND
  GND            GND
  B1             Trim Pot 3
 
*/
 




const int numReadings = 10;    // average sensor reading used from 10 samples

int ledPin = 1;                  // replaced ac/dc relay with led to debug
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = 0;    //reading from sensor


unsigned long start_time;
unsigned long current_time;
unsigned long elapsed_time;

void setup()
{
  // sets digital pin 1 to output
  pinMode(ledPin, OUTPUT);
 
  // sets analog 0 to input
  pinMode(inputPin, INPUT);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;         

}

void loop() {
 
 
 
  // 10 sample average used to prevent false positive from a bounce and/or spike
 
 
 
  // subtract the last reading:
  total= total - readings[index];        
  // read from the sensor: 
  readings[index] = analogRead(inputPin);
  // add the reading to the total:
  total= total + readings[index];      
  // advance to the next position in the array: 
  index = index + 1;                   

  // if we're at the end of the array...
  if (index >= numReadings)             
    // ...wrap around to the beginning:
    index = 0;                          

  // calculate the average:
  average = total / numReadings;        
  // send it to the computer as ASCII digits
   
     delay(1000); // delay in between reads for stability           
 
 
 
  int threshold = 300;                  // sets the threshold value
  if(average > threshold){  // determines if the level of methane breaks threshold
   
    start_time = millis();      // starts exhaust fan on 5 min timer
    digitalWrite(ledPin, HIGH); 
    do  {
      current_time = millis();      // defines the 5 minute timer
      elapsed_time = current_time - start_time;
    }
    while(elapsed_time < 300000); // 5 min. countdown
  }
    if(average < threshold){      // sensor is checked against threshold
      digitalWrite(ledPin, LOW);  // fan turns off if sensor value below threshold
    }                             // or stays on until it is below threshold
 
 
}

http://youtu.be/cm_uuEXd8HM

thanks in advance!

Discussions

0
None
steveastrouk

6 years ago

Here's the fast snippet. It uses something called an exponential forgetter or zero order hold.

The maths is

Current result = last result x forgetterconstant +(current result *(1-forgetterconstant)

Where 0
The code does this with constants based on multiples of 2, so multiply and divides are all done by binary shift operations.

//Val is the current number read from the ADC
//LastVal is the previous ADC number

#define tmfilt 15

Val = analogRead(analogPin);
//Apply an exponential forgetter
LastVal = (LastVal*tmfilt) + (Val<<4) >>4;
//LastVal has filtered number in it
//Now scale to 0..100
LastVal = (LastVal*100)>>10 ;

0
None
earthtownsteveastrouk

Answer 6 years ago

Awesome, I'll give her a whirl. Thanks for the help steveastrouk.

0
None
steveastrouk

6 years ago

The code should fit, but there are faster ways of implementing the kind of filter you have there. I have a piece of Arduino code that's very short and compact, I'll have to dig it out.

0
None
steveastrouk

6 years ago

Have you definitely got the correct target set in the ide ?

0
None
earthtownsteveastrouk

Answer 6 years ago

The pinout in the top of the code is incorrect, the ATtiny85 should be PB0 to led/relay and PB1 to potpin 3.

I had the sensor wired incorrectly it turns out.

I haven't tested the tiny again since realizing this...but as far as you can tell have I exceeded the limitations of the MCU?