0

Anyone ever run into a problem with your attiny not reading your analog pins correctly? (-1023 or 1023 or 0 value) How did you resolve the issue?

Here's what my code looks like now :

4th revision of code as of 5/6/2015 8pm:

int NTC = A1; // thermistor
int switchPin = PB1; // push button
int LED = PB0;  // led 


// button latch
byte ButtonState;
byte lastState = LOW;
byte count = 0;

 //brightness level of each different mode 
int low = 50; 
int med = 140;
int high = 255;             
int dim = 0;


int ADCcount = 0;

void setup() 
{ 

 pinMode(switchPin, OUTPUT);
 pinMode(LED, OUTPUT);  

  }

 void loop() 
 {

  ADCcount = analogRead(NTC);  

delay(100);


  ButtonState = digitalRead(switchPin);

  if(ButtonState && ButtonState != lastState)  // button latch, no debounce      needed.
   {
      if(count < 4) // This will check to see if the count is within a range of  0 - 255 
                 // anything over that, it will reset count back to 0. Of course
      count += 1;  // same as count = count + 5;
      else 
      count == 0;
        }    

    lastState = ButtonState;

   if (count == 0)
  {analogWrite(LED,0);} 
    else if (count == 1) { if(ADCcount < 500) {analogWrite(LED,high);}
      else{ if(ADCcount > 525){analogWrite(LED,low);}
       }            
          } 
      else if (count == 2 ){analogWrite(LED,med);} 
      else if (count == 3) {analogWrite(LED,low);}
      if(count >=4) {count = 0;}
      delay(50);
 }

schematic

simulate this circuit – Schematic created using CircuitLab

  • 1
    Show some effort and indent your code properly. Use four spaces in front of every line of code. I'd do it myself, but I'm on a phone right now. – FRob May 6 '15 at 8:49
  • 1
    350000 is a very big value for microsecond delay. The generated code might overflow and not actually wait or get stuck in an infinite loop. Use millisecond delay instead. – FRob May 6 '15 at 8:52
  • @FRob I switched the delay from microseconds to milliseconds but unfortunately it did not solve the issue.. – Blue Stallion May 6 '15 at 10:29
  • Did you try to run it in debug mode with frequent stops to see the execution flow? I just do not see an infinite loop in your script. What happens after void loop() is executed? – Naz May 6 '15 at 12:04
  • @Naz Um... it loops (the function executes again). The arduino environment hides the standard main() function, declaring but not defining the setup() and loop() functions you see there, instead. – jms May 6 '15 at 12:23
1

The improper bush-button event handling is likely to cause the problem. One way to properly react to the external environment signals could be constant polling or using interrupts. With constant polling, all the time is spent on the checking the status of the button. With interrupts, the program is executed as it should, and only if interrupt happens - the execution jumps to the interrupt subroutine, executes it, and goes back to the main code where it left off.

In your case, you can use pin 7, for example, for push button signal. It can have a function of INT0. Read Section 9. Interrupts of the datasheet - it is straight forward.

I do not completely understand what your program should do, but I will try to convey the general approach of using subroutines:

volatile bool pressed = false;
int main(void)
   {
   MCUCR |= (1<<ISC01 |1<<ISC00);       // set INT0 to interrupt on rising edge of pin7
   sei();                               // enable global interrupts

   while(1){
      if (pressed) then do something; //You can disable interrupts here so your code is not interrupted by cli();
      pressed = false; // reset the state
      else do something else;
   // you can re-enable your interrupts here by sei();
   }        
}

// only happens when the change on pin 7 is from LOW to HI 
ISR(INT0_vect){
pressed = true; // update the pressed status
}

If you want to use pin6 for push button, you should enable the PCINT1 instead of INT0 and change the ISR to handle the PCINT1 interrupt. In this case, pin 6 will react on voltage toggle (from HI to LOW, or the other way) and you can not discriminate rizing/falling edges. In that way, you must read pin6 within the instruction subroutine to know if it is hi or low.

EDIT:

I would suggest to stop the calculations in the Temp() function after the

  average = SERIESRESISTOR / average;

line due to the fact that you have only three operating modes for the LED. Thus you can, back calculate the average value that makes your steinhart value to be, for example >30. This way, all other values greather/smaller than that value will also fall under the same condition depending on whether average/steinhart are directly/inversely proportional.

When you determine the three ranges of raw average values you can use them directly in the Mode() function. Eventually, you can optimize you code even further, by performing monotonic read instead of burst reads. If you keep a buffer, you do only one addition of the most recent sample and subtracting the oldest sample. For this you will need two pointers and one memory for to store sum, but you will not need to do nSamples additions:

read (newSample);
sum += newSample;
sum -= oldestSample;
average = sum/numSamples;
update pointers to the memory for oldest sample and for the feature sample;
| improve this answer | |
  • 350ms should be ample time for the button to settle. Currently, he constantly polls, but when pressing the button for a short amount of time, it should work as is. Of course, pressing and holding it will cycle through all "modes" (counter) without ever giving a reading. – FRob May 6 '15 at 15:53
  • @Naz would I place this code under the void loop ?also should i include the int main? – Blue Stallion May 6 '15 at 15:59
  • @FRob is there a way for when the button is held down it doesnt cycle through all the modes? – Blue Stallion May 6 '15 at 16:02
  • I use Atmel Studio and this is how standard mix of C and assembly is written. I do know what should be written in Arduino environment. – Naz May 6 '15 at 16:05
  • @Naz I downloaded atmel studio yesterday learning how to program with atmel is next on my list. – Blue Stallion May 6 '15 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.