Go Down

### Topic: My first real RC truck project LED Arduino (Read 3418 times)previous topic - next topic

#### Chris_Herrera

##### May 04, 2009, 09:07 am
ok first post first project with the Arduino

#### Chris_Herrera

#1
##### May 04, 2009, 09:08 am
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
http://arduino.cc/en/Tutorial/Loop
please forget me but i have not got a bread board yet.
here is a HD pic
http://i380.photobucket.com/albums/oo241/Googlefishchris/100_8816.jpg

now here is a video of the uploaded Knight Rider code.
http://www.youtube.com/watch?v=-6t_dATtc_o

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.
http://www.youtube.com/watch?v=RKFXIdBLrbA

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
http://www.ladyada.net/learn/arduino/index.html

#### flickerfly

#2
##### May 09, 2009, 07:19 pm
Great start, but Dude, you gotta learn about functions! They'll save you a LOT of time, and increase your fun. :-)

http://www.arduino.cc/en/Reference/FunctionDeclaration

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. :-)
Code: [Select]
`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. :-)

#### Chris_Herrera

#3
##### May 09, 2009, 11:51 pm
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.

#### flickerfly

#4
##### May 10, 2009, 05:20 am
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

#### Chris_Herrera

#5
##### May 10, 2009, 09:22 am
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
}

#### k2pek2

#6
##### May 11, 2009, 01:17 am
Quote

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.

#### Chris_Herrera

#7
##### May 11, 2009, 04:28 am
well like i said i cant real understand till i see the code side by side, im a noobe

#### Chris_Herrera

#8
##### May 11, 2009, 05:32 am
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.

#### mem

#9
##### May 11, 2009, 06:56 amLast Edit: May 11, 2009, 06:59 am by mem Reason: 1
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.

Code: [Select]
`// connect leds to pins 2 through 5// loop repeats right sequence twice, followed by left sequence repeated twiceint timer = 100;                   // The higher the number, the slower the timing.int firstPin = 2;                  // the first pin number for the dwquentioal pins used for LEDsint num_pins = 3;                  // the number of pins used for LEDsint turnSequenceLength = 3; // the number of elements in the following arraysint 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!

#### Chris_Herrera

#10
##### May 13, 2009, 07:14 am
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 ?

#### mem

#11
##### May 13, 2009, 12:01 pm
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:

Code: [Select]
`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
[font=Courier New]  int leftSequence[] =  { 2,1,0};[/font]
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

#### Chris_Herrera

#12
##### May 14, 2009, 03:01 am
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.

#### zooto68

#13
##### May 14, 2009, 03:49 am
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.

#### Chris_Herrera

#14
##### May 15, 2009, 05:11 am
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.
Code: [Select]
`int timer = 100;                   // The higher the number, the slower the timing.int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbersint 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 rightdigitalWrite(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 leftdigitalWrite(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

Go Up