Go Down

Topic: blinking brake lights for RC cars not working in ATtiny45 (Read 888 times) previous topic - next topic

mechatron

Jan 11, 2012, 11:54 pm Last Edit: Jan 12, 2012, 12:04 am by mechatron Reason: 1
Hi,

I have made a code for blinking brake lights.

The code works fine with my ATmega1280 as can be seen here:
http://www.youtube.com/watch?v=thAVbtShQUc

When I upload the code in an ATtiny45 it doesn't work. When the transmitter is off the led slowly blinks and when I turn the transmitter on the led just fades and does nothing.
When the transmitter is turned on again the led starts blinking slowly again.

Here's the code:
Code: [Select]


const int ledPin = 0;                      // number of the LED pin

int ledState = LOW;
int t;
int x;
int y;
int z;

long previousMillis = 0;                     // will store last time LED was updated

                                            // the follow variables is a long because the time, measured in miliseconds,
                                            // will quickly become a bigger number than can be stored in an int.
long interval = 100;                         // interval at which to blink (milliseconds)

void setup() {
 pinMode(ledPin, OUTPUT);                   // set the digital pin as output:
 t = 0;
 z = 1400;
}

void loop()
{
 y = pulseIn(1, HIGH);                      // read PPM signal on ch.2 from receiver
   if(y > z){
     x = 1;
   }
   else
   if(y <= z){
     x = 0;
   }

unsigned long currentMillis = millis();

if(x == 0 && t == 0){
  previousMillis = currentMillis;
  ledState = HIGH;
  digitalWrite(ledPin, ledState);
  t = 1;
}

if(x == 0 && t == 1 && currentMillis - previousMillis > interval && ledState == HIGH){
 previousMillis = currentMillis;
 ledState = LOW;
 digitalWrite(ledPin, ledState);
}

if(x == 0 && t == 1 && currentMillis - previousMillis > interval && ledState == LOW){
 previousMillis = currentMillis;
 ledState = HIGH;
 digitalWrite(ledPin, ledState);
}

if(x == 1 && t == 1){
 t = 0;
 if(ledState == HIGH){
   ledState = LOW;
   digitalWrite(ledPin, ledState);
 }
}
}


What am I doing wrong?

Leo
to boldly code what no man has coded before

Coding Badly

Hi,

First, some general things...

[font=Courier New]unsigned long previousMillis = 0;                     // will store last time LED was updated[/font]

[font=Courier New]static const unsigned long interval = 100;                         // interval at which to blink (milliseconds)[/font]


Now to the problem... What is the ATtiny45 processor speed?

mechatron

Hi,

It should be 8MHz because I "burned" the bootloader.

to boldly code what no man has coded before

Coding Badly


The most likely culprit is internal oscillator.  The ±10% that Atmel guarantees may be too much for your application to function correctly.  You can either fiddle with the threshold (z) or tune the oscillator.

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