Just getting started...is this possible with ATtiny85?

Hello,

My son decided he wanted to build his pinewood derby car this year based on the Ghostbusters Ecto-1 car. He wants flashing lights, sirens, the whole deal. The problem is that the car must weigh under 5oz to be legal. I started searching and looking for ideas and came across a guy who built a police pinewood derby car with flashing lights using Arduino as a programmer for an ATtiny85 chip. He wrote a program which would alternate the flashing of a red and blue LED on the roof and would also control the 4 LEDs used for head-lights and 2 LEDs used for tail-lights. Almost exactly what I needed! The probem is that my son would also like it to have some sort of police siren. As I started searching more I found that ATtiny85 does not natively support the tone function, but think maybe there is a way to enable it?

Anyway, I am totally new arduino (but not programming) and have the following questions:

  • Using the ATtiny85 is it possible to control a set of headlights/taillights (always on), 1 red and 1 blue LED (alternating blink) and a piezo speaker (alternating tones)? Are there enough outputs?
  • What do I need to put this all together other than the obvious parts (battery, leds, attiny85 chip, etc)
  • How do I alter this program to add the tones if it can be done? (program below)

Thanks for all the help in advance. I am really excited to get started with this new adventure with my son and maybe learn a few new things along the way!

// Code for pinewood derby cop car
// This code is for the ATtinny85 chip
// To modify the code to work for any other chip all that
// is needed is to change the pin numbers
int blue = 3;      // Blue LED on top of car         
int red = 2;       // Red LED on top of car       
int headLight = 1; // Four LEDs in front of car wired in parallel
int tailLight = 4; // Two LEDs in rear of car wired in parallel


void setup(){
  pinMode(blue, OUTPUT);       
  pinMode(red, OUTPUT);     
  pinMode(headLight, OUTPUT);
  pinMode(tailLight, OUTPUT);
 
}
void loop(){
// This will keep the headlights and tail lights on
// and blink the red and blue lights like a real police car
  digitalWrite(headLight, HIGH);
  digitalWrite(tailLight, HIGH);
  int num1 = 0;
  while(num1 < 2){ 
    digitalWrite(blue, HIGH);
    delay(75);
    digitalWrite(blue, LOW);
    delay(75);
    num1++;
  }
  int num2 = 0;
  while(num2 < 2){
    digitalWrite(red, HIGH);
    delay(75);
    digitalWrite(red, LOW);
    delay(75);
    num2++;
  } 
}

I think you should be able to do this. I don't think you need to use 2 outputs though for your headlights though, since they are always on?

jimmy

HI, sounds good, but don't forget the more outputs you have the more power you are going to need, not sure how long a derby is but battery mass must also be considerd.

Tom..... :)

Thanks guys. I plan to put a switch inline so I can turn it on and off between races. So I can tie the tail-lights and head-lights to a single output because they will always be on, that makes sense. Do I just wire them in all in parallel? Where do I go from here? How can I alter my code to add the siren “sound”? I am guessing I can send alternating notes to get the effect, but am not sure what to do exactly. The schematic I was working with is as follows…how do i wire in the piezo speaker to this and get it all working?

Adafruit had a how to on how to do tone generation on an ATtiny85: http://learn.adafruit.com/trinket-gemma-mini-theramin-music-maker

I wouldn’t hook the headlights and taillights through the microcontroller at all. Just hook them
up to the battery with a current limiting resistor.

Jimmy

electricd7: The probem is that my son would also like it to have some sort of police siren. As I started searching more I found that ATtiny85 does not natively support the tone function, but think maybe there is a way to enable it?

This core has always supported tone on the ATtiny85 processor... http://code.google.com/p/arduino-tiny/

As an added bonus if you use an Output Compare register connected to timer 0 the tone is generated entirely by hardware (quality is better; no processor load).

How much current do the lights in your schematic use? Or are those meant to be LEDs?

The "lights" in the schematic are actually LEDs, they were just labeled wrong. Right, so the headlight/taillights can go directly to battery since they wont be turning off/on or anything fancy. That frees up 2 outputs in the ATtiny. I assume the peizo only needs a single output, so I should be fine. I will do some more reading and try to figure out how to make this all work and then post an updated program for review. I plan to use a an old Nokia cell phone battery to run all of this because its flat and fairly small and rechargeable, so he can play with it after the race.

OK, so thinking through the programming of this, I am having a hard time figuring out how to make the lights flash in the intended pattern (see above program) while beeping the peizo in pattern as well. I can only have a single loop playing at one time, correct? Seems like I may have to use two of the ATtiny85 chips to accomplish what I want. Am I missing something?

No, you will only need the one chip. Have a look at the “blink without delay” example in the learning section.

Paul

I just came across your post. I just built an Ecto-1 Pinewood Derby Car with sound and flashing lights to include headlights, tail lights and light bars on top. It was tricky to get the weight under 5 oz but I was able to do it. I did not get the sound working as I would have liked to because I ran out of time but the kids and adults were still impressed.

I was planing to use an Adafruit Trinket which is based on the ATtiny85 but ran into the same issue with sound. I was able to get sound working using one of the examples on Adafruit. I decided I really wanted to use tone so I decided to use one of the Adafruit ATMega328P with the bootloader already loaded. This gave me the advantage of being able to control the headlights and tail lights individually if I wanted plus the tone function works on this chip. (Yes, I could have modified the core so that tone would work with the ATtiny85 but being the procrastinator that I am, I did not have time to find it and update it.)

Here is a list of components that I used. (This is off the top of my head)

  • ATMega328P
  • LM317 Voltage Regulator (Resistors used for 5VDC output)
  • Resistors and capacitors
  • 1N4001 Diode
  • Radio Shack (230-0336 ) 12VDC 23A Battery
  • 4 LEDs (2 blue for headlights & 2 red for tail lights)
  • 6 Adafruit Neopixels (for top light rails)

I may not have programmed this the best way possible but I got it to work quickly. Here is my code. (You will needed to add pitches.h as directed at http://arduino.cc/en/Tutorial/tone)

#include "pitches.h"
#include <Adafruit_NeoPixel.h>

#define SPEAKER_PIN 8
#define NEOPIXEL_PIN 6
#define L_FRONT_LED_PIN 10
#define R_FRONT_LED_PIN 11
#define L_REAR_LED_PIN 12
#define R_REAR_LED_PIN 13

int notes[] = {
  NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_D3, NOTE_C3, NOTE_F3, NOTE_F3, 0, NOTE_B3, 0, NOTE_B3, 0, NOTE_F3, NOTE_F3, 0, NOTE_B3, 0, NOTE_B3, 0, NOTE_F3, NOTE_F3, 0, NOTE_B3, 0, NOTE_B3, 0};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int durations[] = {
  16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 4, 4, 8, 8, 16, 8, 16, 4, 4, 8, 8, 16, 8, 16, 4, 4, 8, 8, 16, 8, 16};

Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);

int notes_length = sizeof(notes) / sizeof(int);
boolean lights_on = false;
boolean start_blue = false;

void setup() {
  //neopixel_setup();
  strip.begin();
  strip.setBrightness(60);
  strip.show(); // Initialize all pixels to 'off'
  
  //setup_body_lights();
  pinMode(L_FRONT_LED_PIN, OUTPUT);
  pinMode(R_FRONT_LED_PIN, OUTPUT);
  pinMode(L_REAR_LED_PIN, OUTPUT);
  pinMode(R_REAR_LED_PIN, OUTPUT);
}

void loop() {
  /*
  test_sound();
   test_neopixels();
   test_body_lights();
   */
  for (int thisNote = 0; thisNote < notes_length; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/durations[thisNote];
    tone(SPEAKER_PIN, notes[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(SPEAKER_PIN);
    
    all_lights_on_off(lights_on);
    lights_on = !lights_on;
    
    for(int i=0; i< strip.numPixels(); i++) {
      if(start_blue) {
        if(i % 2 == 1) {
          strip.setPixelColor(i, strip.Color(255, 0, 0));  // Red
        }
        else {
          strip.setPixelColor(i, strip.Color(0, 0, 255));  // Blue
        }
      }
      else {
        if(i % 2 == 1) {
          strip.setPixelColor(i, strip.Color(0, 0, 255));  // Blue
        }
        else {
          strip.setPixelColor(i, strip.Color(255, 0, 0));  // Red
        }
      }
    }
    strip.show();
    start_blue = !start_blue;
  }
}



void all_lights_on_off(boolean lights_on) {
  if(lights_on) {
    digitalWrite(L_FRONT_LED_PIN, HIGH);
    digitalWrite(R_FRONT_LED_PIN, HIGH);
    digitalWrite(L_REAR_LED_PIN, HIGH);
    digitalWrite(R_REAR_LED_PIN, HIGH);
  }
  else {
    digitalWrite(L_FRONT_LED_PIN, LOW);
    digitalWrite(R_FRONT_LED_PIN, LOW);
    digitalWrite(L_REAR_LED_PIN, LOW);
    digitalWrite(R_REAR_LED_PIN, LOW);
  }
}

If I get around to it, I may do this again and add an entry to www.teelsys.com.

Hope you got it to work. I have been looking for the Ecto-1 sound so I can program the correct sound but have not found anything. I do have an idea to use another Arduino with a mic to listen to the sound playing and output notes and durations. I don’t know if that will work well or if I will get around to it. This was my son’s last year in Cub Scouts so we will not be racing next year.

I forgot to mention that I showed this on Adafruit's Show-and-Tell this past Saturday (1/18/2014). The video is available at http://www.adafruit.com/blog/2014/01/18/show-and-tell-1182014-video/ and on YouTube at http://www.youtube.com/watch?v=sH_cmJFe1ds. (~9:50 mark)

Awesome! We did actually get it to work. I wasn't able to do the sound because I couldn't get the car any leaner in weight (5oz tops). I ended up powering the whole thing with a CR2032 which lasted amazingly much longer than I expected. I don't have a video at the moment, but I will post a picture of how it turned out. He won best of show and was thrilled with how much interest his car received from the other scouts and parents.

Congratulations. That thrill of interest from others is what keeps most of us making projects.

Hi, Congratulations on the project, I googled google'd (I dunno) pinewood derby, I like the concept. Make a car in an afternoon, any creative young mind could go wild. Question do the regs allow you to hollow out the car to save weight and hide some of the features? Car looks great.. 8)

Tom.... :)

Yea, I actually did hollow out almost the entire inside of that car. The car by itself weighed just over 2oz. The other 3 oz was made with the axels, wheels, lights, wires, battery ,etc.

Bumping this thread.

I would really appreciate being able to see how your final solution looked. I have a similar idea for a Pinewood Derby car (I have some 3.3v Trinkets that need something to do) and have nothing started yet.

Thanks.