Pages: [1]   Go Down
Author Topic: blinking brake lights for RC cars not working in ATtiny45  (Read 818 times)
0 Members and 1 Guest are viewing this topic.
Tilburg, Noord Brabant
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have made a code for blinking brake lights.

The code works fine with my ATmega1280 as can be seen here:


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:

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
« Last Edit: January 11, 2012, 06:04:06 pm by mechatron » Logged

to baldly code what no man has coded before

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 208
Posts: 12936
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

First, some general things...

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

static const unsigned long interval = 100;                         // interval at which to blink (milliseconds)


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

Tilburg, Noord Brabant
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

Logged

to baldly code what no man has coded before

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 208
Posts: 12936
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Pages: [1]   Go Up
Jump to: