Pages: [1]   Go Down
Author Topic: Simplyfing Code?  (Read 702 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I have created a knight rider led thing, like scrolling leds lol
and I have seen other codes use alot less then what i used, so i have tried to uderstand it but i cant, I included my code for the one i created i wanted to know how to simplify it and if you could do it for me and explain in?  I want to know how, I am new to all this, I bought one mega and recieved two! go me, so i placed them on a small stand i created with a breadboard all held together for easy prototyping, so i wanted to now begin using them....also how could i create like a sequence, would i use "for" or "char" would i declare the led cluster as  matrix[] (3, 4, 5, 6, 7); or how?

Code:
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;
int incomingByte;    
int buttonState = 0;


void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  Serial.print("\nLed Controller");
}

void loop() {
  Serial.print("\n Sequence 1");
  analogWrite(led1, 100);
  analogWrite(led7, 100);
  delay(100);
  analogWrite(led2, 100);
  analogWrite(led6, 100);
  analogWrite(led1, 0);
  analogWrite(led7, 0);
  delay(100);
  analogWrite(led2, 0);
  analogWrite(led6, 0);
  analogWrite(led3, 100);
  analogWrite(led5, 100);
  delay(100);
  analogWrite(led4, 100);
  analogWrite(led3, 0);
  analogWrite(led5, 0);
  delay(100);
  analogWrite(led4, 0);
  analogWrite(led3, 100);
  analogWrite(led5, 100);
  delay(100);
  analogWrite(led2, 100);
  analogWrite(led6, 100);
  analogWrite(led3, 0);
  analogWrite(led5, 0);
  delay(100);
  analogWrite(led1, 100);
  analogWrite(led7, 100);
  analogWrite(led2, 0);
  analogWrite(led6, 0);
  delay(100);
  analogWrite(led1, 0);
  analogWrite(led7, 0);
  analogWrite(led1, 100);
  analogWrite(led7, 100);
  delay(100);
  analogWrite(led2, 100);
  analogWrite(led6, 100);
  analogWrite(led1, 0);
  analogWrite(led7, 0);
  delay(100);
  analogWrite(led2, 0);
  analogWrite(led6, 0);
  analogWrite(led3, 100);
  analogWrite(led5, 100);
  delay(100);
  analogWrite(led4, 100);
  analogWrite(led3, 0);
  analogWrite(led5, 0);
  delay(100);
  analogWrite(led4, 0);
  analogWrite(led3, 100);
  analogWrite(led5, 100);
  delay(100);
  analogWrite(led2, 100);
  analogWrite(led6, 100);
  analogWrite(led3, 0);
  analogWrite(led5, 0);
  delay(100);
  analogWrite(led1, 100);
  analogWrite(led7, 100);
  analogWrite(led2, 0);
  analogWrite(led6, 0);
  delay(100);
  analogWrite(led1, 0);
  analogWrite(led7, 0);
  analogWrite(led1, 100);
  analogWrite(led7, 100);
  delay(100);
  analogWrite(led2, 100);
  analogWrite(led6, 100);
  analogWrite(led1, 0);
  analogWrite(led7, 0);
  delay(100);
  analogWrite(led2, 0);
  analogWrite(led6, 0);
  analogWrite(led3, 100);
  analogWrite(led5, 100);
  delay(100);
  analogWrite(led4, 100);
  analogWrite(led3, 0);
  analogWrite(led5, 0);
  delay(100);
  analogWrite(led4, 0);
  analogWrite(led3, 100);
  analogWrite(led5, 100);
  delay(100);
  analogWrite(led2, 100);
  analogWrite(led6, 100);
  analogWrite(led3, 0);
  analogWrite(led5, 0);
  delay(100);
  analogWrite(led1, 100);
  analogWrite(led7, 100);
  analogWrite(led2, 0);
  analogWrite(led6, 0);
  delay(100);
  analogWrite(led1, 0);
  analogWrite(led7, 0);

}
« Last Edit: September 07, 2010, 10:08:21 pm by collazo2929 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are using analogWrite, which enables PWM, on non PWM pins. There are only 6 PWM pins on the Duemilanove. They are 3, 5, 6, 9, 10, and 11.

Using an array,
Code:
ledPins[] = {2, 3, 4, 5, 6, 7, 8};
would be the first step.

Create some functions called Step1(), Step2(), etc. Each function defines one frame of the animation. Then, call the functions in loop:
Code:
void loop()
{
   Step1();
   Step2();
   Step3();
   Step4();
   Step4();
   Step3();
   Step2();
   Step1();
}

void Step1()
{
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[6], HIGH);
  delay(100);
}
The rest of the functions are easy...
« Last Edit: September 08, 2010, 05:25:19 am by PaulS » Logged

whistler
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

Havn't tried this but it may give you the idea. Of course you could get smart and use pwm on your MEGA (pins 0..13) and fade them in and out.

Code:
byte ledPins[] = {2, 3, 4, 5, 6, 7, 8};
byte left = 0;
byte right = sizeof(ledPins)/sizeof(byte);

void setup()
{
  for (byte i=0; i<right; i++)
  {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop()
{
  digitalWrite(ledPins[left], HIGH);
  digitalWrite(ledPins[right - left - 1], HIGH);
  delay(100);
  digitalWrite(ledPins[left], LOW);
  digitalWrite(ledPins[right - left - 1], LOW);
  left = ++left % right; //restrict left between 0 and 6 (in this case)
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks, ok so if i want a certain action while a button is pressed would do


::

if {buttonState == HIGH}{
animation1();

::
\\this is just an example not exactly correct, but if i were to do that and wanted that process to continue to happen until another button was pressed or the same one pressed how would i do that? because i have tried and say ""animation1"" only happens while the button is pressed, do i insert a delay?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want an action to start if a button is pressed, and continue until another button is pressed, you need to separate the button press reading from the action.

When the button is pressed, set a flag indicating which action is to occur.

After reading all the buttons, perform the correct action.

Code:
int action = 0;
void loop()
{
   if(digitalRead(buttonPin1) == HIGH)
      action = 1;
   if(digitalRead(buttonPin2) == HIGH)
      action = 2;

   switch(action)
   {
      case 1:
         animation1();
         break;
      case 2:
         animation2();
         break;
      default:
         break;
   }
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how does ""break;"" work?
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5551
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how does ""break;"" work?

I'll reference PaulS's example:

Say action was equal to 1; so the first "case 1:" block would be selected, and animation1() would be called, then the break would hit, and the program would continue (back to the top of the loop, since there is nothing after the switch-case construct). The break keeps the system from executing the second "case 2:" block.

Now, this is important - especially in a switch-case construct; in this kind of control system, you can have "fall-thru" logic. Let's say that instead of the way it is currently written, it instead looked like this:

Code:
  switch(action)
   {
      case 1:
         animation1();
         break;
      case 2:
           // break intentionally omitted
      default:
           animation2();
         break;
   }

Now - if action equals 2, or any other value other than 1, animation2() would be called. In other words, if action is 2, the logic "falls thru" the "case 2:" block and on to the "default:" block. This is an important thing to understand - you should always have a default handler in your switch-case control block, so that is a value is for some reason outside a range or whatever, something will be executed (even if it is a call to the serial.print() function outputting an error message!). If your default is something that will happen for one of the case values, then you should use a fall-thru construct instead; it makes the code smaller and less redundant. However, only you can decide when to use it; it isn't appropriate in every switch-case to use a fall-thru for the construct. Note that you can also do something like this:

Code:
  switch(action)
   {
      case 1:
         animation1();
           // break intentionally omitted
      case 2:
           animation2();
           // break intentionally omitted
        case 3:
           animation3();
           break;
        case 4:
           // break intentionally omitted
      default:
           animationDefault();
         break;
   }

See how that fall-thru works? So you can get tricky!

Now, you might be wondering about those comments I put in there - those have a purpose to. They are there to tell others (or your future forgetful self!) that you -purposefully- meant to leave out the break, and that putting one in there may (will) change the logic of the construct, so its a reminder to either "be careful, or be warned"...

Hope this helps explain things!

 smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay yea i see how that works, i will try to apply this on my next project, I am going to try to create a sign like the red scrolling ones

::::::::::    they loook like that and are made of led's, so a matrix of led's
how shoudl i go about this? shift registers? or just one led per pin on the mega? but wouldnt that be too hard?
and also would i have to write a library or how ?
 :-/
Logged

Pages: [1]   Go Up
Jump to: