My first real RC truck project LED Arduino

ok first post first project with the Arduino

My wife got me this RC H3. I started off with giving it head lights (blue leds) and tail lights (red leds). its is powered by a 6v so i just taped in after the switch.
Then i got my Arduino off ebay and set up 6 led lights on the tail gate and used the code for the "Knight Rider" project

please forget me but i have not got a bread board yet.
here is a HD pic

now here is a video of the uploaded Knight Rider code.

Cool hu. ok well I took some time and write my own code (mod the the first one) I'm very new to code and Arduino. Let me put it this way 3 days ago i got my Arduino. I was done with the project last night. I have never before study or read or write code of any kind before. Take a look at my fished project. As you will see it was worth taking the time to write the code. I'm sure there was some way ezer way to write the code but like i said i have never done this before so please feel free to give me some feed back.

int timer = 100; // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6; // the number of pins (i.e. the length of the array)

void setup()
{
int i;

for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
pinMode(pins*, OUTPUT); // set each pin as an output*
}
void loop()
{
int i;
for (i = 0; i < num_pins; i++) { // loop through each pin...

}

//turn right
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[5], HIGH);
delay(100);
digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[5], LOW);
delay(100);
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[5], HIGH);
delay(100);
digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[5], LOW);
delay(100);
//turn left
digitalWrite(pins[2], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[2], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[0], LOW);
delay(100);
digitalWrite(pins[2], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[2], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[0], LOW);
delay(100);
//DEMO
digitalWrite(pins[5], HIGH);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[3], HIGH);
digitalWrite(pins[2], HIGH);
delay(100);
//2
digitalWrite(pins[5], LOW);
digitalWrite(pins[0], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[3], LOW);
digitalWrite(pins[2], LOW);
delay(100);
//HH
digitalWrite(pins[5], HIGH);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[3], HIGH);
digitalWrite(pins[2], HIGH);
delay(100);
//2
digitalWrite(pins[5], LOW);
digitalWrite(pins[0], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[3], LOW);
digitalWrite(pins[2], LOW);
delay(100);
// NEXT DEMO
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[5], HIGH);
delay(100);
digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[5], LOW);
delay(100);
//
digitalWrite(pins[5], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[2], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[0], HIGH);
delay(100);
//
digitalWrite(pins[5], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[2], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[0], LOW);
delay(100);
//
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[2], HIGH);
delay(100);
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[5], HIGH);
delay(100);
//
digitalWrite(pins[0], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[2], LOW);
delay(100);
digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[5], LOW);
delay(100);
//
digitalWrite(pins[5], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[2], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[0], HIGH);
delay(100);
//
digitalWrite(pins[5], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[2], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[0], LOW);
delay(100);
//BEEP
digitalWrite(pins[5], HIGH);
digitalWrite(pins[4], HIGH);
digitalWrite(pins[3], HIGH);
digitalWrite(pins[2], HIGH);
digitalWrite(pins[1], HIGH);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[5], LOW);
digitalWrite(pins[4], LOW);
digitalWrite(pins[3], LOW);
digitalWrite(pins[2], LOW);
digitalWrite(pins[1], LOW);
digitalWrite(pins[0], LOW);
delay(100);
digitalWrite(pins[5], HIGH);
digitalWrite(pins[4], HIGH);
digitalWrite(pins[3], HIGH);
digitalWrite(pins[2], HIGH);
digitalWrite(pins[1], HIGH);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[5], LOW);
digitalWrite(pins[4], LOW);
digitalWrite(pins[3], LOW);
digitalWrite(pins[2], LOW);
digitalWrite(pins[1], LOW);
digitalWrite(pins[0], LOW);
delay(100);
digitalWrite(pins[5], HIGH);
digitalWrite(pins[4], HIGH);
digitalWrite(pins[3], HIGH);
digitalWrite(pins[2], HIGH);
digitalWrite(pins[1], HIGH);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[5], LOW);
digitalWrite(pins[4], LOW);
digitalWrite(pins[3], LOW);
digitalWrite(pins[2], LOW);
digitalWrite(pins[1], LOW);
digitalWrite(pins[0], LOW);
delay(100);
digitalWrite(pins[5], HIGH);
digitalWrite(pins[4], HIGH);
digitalWrite(pins[3], HIGH);
digitalWrite(pins[2], HIGH);
digitalWrite(pins[1], HIGH);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[5], LOW);
digitalWrite(pins[4], LOW);
digitalWrite(pins[3], LOW);
digitalWrite(pins[2], LOW);
digitalWrite(pins[1], LOW);
digitalWrite(pins[0], LOW);
delay(100);
}
this site helped me alot
Arduino Tutorial - Learn electronics and microcontrollers using Arduino!

Great start, but Dude, you gotta learn about functions! They'll save you a LOT of time, and increase your fun. :slight_smile:

Basically, a function is a bit of code you can re-use as much as you like. So you can code something once and reuse it instead of writing it several times over, like you did in your code.

You could also setup lights to blink as a right turn signal and a left turn signal. You'd call the function something like TurnSignal("left") and TurnSignal(right). Then in your loop(), which is a function itself, you may take some sort of input from a sensor that can tell when the wheels of your RC truck are turning and call your turnSignal() function with either the left or right argument. Then inside that you could use the case statement (http://www.arduino.cc/en/Reference/SwitchCase) to identify which argument and blink the appropriate lights.

That's be a pretty simple extension of your idea here. Hope it helps you continue in your fun. :slight_smile:

setup(){ /*something*/ }

loop(){ 
// set up the loop to always watch for a pin to go HIGH, meaning it is on 
// (I'm assuming a fictional sensor connected to this pin) and this will be 
// used to tell the turn signal to flash the right or left
  if (digitalRead(8) == HIGH) turnSignal("right")
  if (digitalRead(9) == HIGH) turnSignal("left")
}

void turnSignal(string direction) { //the turnSignal function takes an
// argument that is a string with a variable named direction, this isn't
// blinking, but you get the idea.
  switch (direction) {
    case "left":
        digitalWrite(pins[5], HIGH);
        digitalWrite(pins[4], HIGH);
        digitalWrite(pins[3], HIGH);
    case "right":
        digitalWrite(pins[5], HIGH);
        digitalWrite(pins[4], HIGH);
        digitalWrite(pins[3], HIGH);
  }

Oh, I haven't tested that code. It probably has errors, but is meant to explain the flow, not exactly how to do it. :slight_smile:

I kind of get what your saying but im having a hard time making it come together. I know its asking alot but would you try to rewrite my code so it dose the same thing but in the way you were telling me? a side by side look at both codes would help me understand how to do this much better.

Hmm... First I'd have to understand what you were accomplishing in each piece. Perhaps we could split the difference. Let me submit a similar project written using functions. The code is up for display on my blog. Feel free to ask me questions about it.

http://josiahritchie.com/blog/arduino-project-video-advanced-blinky-lights

I took a look and its like half of it make cents to me and the rest i just dont get
i get it till about here
void setup() // run once, when the sketch starts
{
for (count=0; count<6; count++) // setup the pins for output
{
pinMode(lightArray[count], OUTPUT);
}
Serial.begin(9600); // tell arduino to start up serial comms
}

void setup() // run once, when the sketch starts
{
for (count=0; count<6; count++) // setup the pins for output
{
pinMode(lightArray[count], OUTPUT);
}
Serial.begin(9600); // tell arduino to start up serial comms
}

The for line makes the variable count go through all 6 times (rather than declaring pinmode(lightArray[1], OUTPUT), then the next line would be lightArray[2], etc), and the line starting with pinMode sets the data indexed as count in the array "lightArray" as an output pin.

Serial.begin(9600) starts the arduino talking to a computer at a set rate.

well like i said i cant real understand till i see the code side by side, im a noobe

ok im really trying and getting no where, I just what to write a case" for this

int timer = 100; // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6; // the number of pins (i.e. the length of the array)

void setup()
{
int i;

for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
pinMode(pins*, OUTPUT); // set each pin as an output*
}
void loop()
{

  • int i;*

  • for (i = 0; i < num_pins; i++) { // loop through each pin...*

  • }*

//turn right
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[5], HIGH);
delay(100);
digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[5], LOW);
delay(100);
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[5], HIGH);
delay(100);
digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[5], LOW);
delay(100);
///////////////////
can you show me just when this part how to turn all this code to make it blink right into a case so i can just type case "right"
i dont know what im dont wrong but its just not working i mite be putting things in the wrong place.

Here is some (untested) code that gives you an idea of how you can proceed.

Note that the sequence arrays hold an index to the leds you have connected. If your first LED is connected to pin 2, then the index 0 indicates pin 2, index 1 pin 3, index 2 pin 4 etc.

// connect leds to pins 2 through 5
// loop repeats right sequence twice, followed by left sequence repeated twice

int timer = 100;                   // The higher the number, the slower the timing.
int firstPin = 2;                  // the first pin number for the dwquentioal pins used for LEDs
int num_pins = 3;                  // the number of pins used for LEDs

int turnSequenceLength = 3; // the number of elements in the following arrays
int rightSequence[] = { 1,2,3}; // the pin index to use , 0 is the first of your CONNECTED pins (pin 2 in this example)
int leftSequence[] =  { 2,1,0}; // this is pin 4, pin 3, pin 2 (because 0 is pin 2, 1 is pin 3 etc) 

void setup()
{
  int i;

  for (i = 0; i < num_pins; i++)   // the array elements are numbered from 0 to num_pins - 1
    pinMode(firstPin + i, OUTPUT);      // set each pin as an output
}

void loop()
{
   sequenceLEDS(rightSequence, turnSequenceLength, 2); // do right turn sequence two times
   delay(1000);
   sequenceLEDS(leftSequence, turnSequenceLength, 2); // do right turn sequence two times
   delay(2000);
}

void sequenceLEDS( int *sequence , int length, int repeats)
{
  for( int counter=0; counter < repeats; counter++) 
  {
    for( int i = 0; i < length; i++)
    {
      digitalWrite(sequence[i],HIGH);
      delay(100);  
    }
    for( int i = 0; i < length; i++)
    {
      digitalWrite(sequence[i],LOW);
      delay(100);  
    }
  }    
}

You may need to play with the sequence function to get it to do exactly what you want but I hope it gives you an idea of how to proceed.

have fun!

you lost me here

int rightSequence[] = { 1,2,3}; // the pin index to use , 0 is the first of your CONNECTED pins (pin 2 in this example)
int leftSequence[] = { 2,1,0}; // this is pin 4, pin 3, pin 2 (because 0 is pin 2, 1 is pin 3 etc)

pin0 is pin2 ?

Its confusing because I left out a key piece of code in that post.

The sequenceLEDS routine should have added the firstPin value to the digital writes:

void sequenceLEDS( int *sequence , int length, int repeats)
{
  for( int counter=0; counter < repeats; counter++)
  {
    for( int i = 0; i < length; i++)
    {
      digitalWrite(sequence[i]+ firstPin ,HIGH);
      delay(100);  
    }
    for( int i = 0; i < length; i++)
    {
      digitalWrite(sequence[i]+ firstPin,LOW);
      delay(100);  
    }
  }    
}

This is still untested, but for example, with
int leftSequence[] = { 2,1,0};
this should do the following:

  • digital write to firstPin + 2 (pin 4)
  • digital write to firstPin + 1 (pin 3)
  • digital write to firstPin + 0 (pin 2)

I hope that helps

posting only half cods and cods that mite not work is not helping at all. I think you much not understand that i em very new to all this and dont under stand every thing. so thats where im at.

Pllleeeeeeeeeeeeeaaaaaasssssseeeeeeee post code inside the code quotes. Just click the littel hash icon when you are making your post and then paste your code where the cursor is located. That makes a nice code window with scrollbars so we don't have to scroll down acres of window to see your code.

Thanks.

ok here is the code i would like to see made in to functions. Please like i said im very new at this so no half cods or un checked codes. If you post it, make sure it works first. also please // and tell what each line of code dose. Thank you and if you need any more info please let me know.

int timer = 100;                   // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6;                  // the number of pins (i.e. the length of the array)

void setup()
{
  int i;

  for (i = 0; i < num_pins; i++)   // the array elements are numbered from 0 to num_pins - 1
    pinMode(pins[i], OUTPUT);      // set each pin as an output
}

void loop()
{
  int i;

  for (i = 0; i < num_pins; i++) { // loop through each pin...
   
  }
  
    
//turn right
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[5], HIGH);
delay(100);

digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[5], LOW);
delay(100);
digitalWrite(pins[3], HIGH);
delay(100);
digitalWrite(pins[4], HIGH);
delay(100);
digitalWrite(pins[5], HIGH);
delay(100);

digitalWrite(pins[3], LOW);
delay(100);
digitalWrite(pins[4], LOW);
delay(100);
digitalWrite(pins[5], LOW);
delay(100);
//turn left
digitalWrite(pins[2], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[2], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[0], LOW);
delay(100);
digitalWrite(pins[2], HIGH);
delay(100);
digitalWrite(pins[1], HIGH);
delay(100);
digitalWrite(pins[0], HIGH);
delay(100);
digitalWrite(pins[2], LOW);
delay(100);
digitalWrite(pins[1], LOW);
delay(100);
digitalWrite(pins[0], LOW);
delay(100);




  }

Thank you