Simplifying code

I am working an EL wire project using an EL sequencer that is being programmed as a Lilypad w/ ATMega328. Being Relatively new to arduinos I have a limited understanding of code and am trying to trim and simplify the code to allow more space on the program. I have 3 particular problems that I am trying to solve and am looking to the community to help me out.

  1. A simpler way to define a means to turn a pin on and off instead of having to write out elSegment(int,value) for each one.
  2. A way to include a set number of loops within the main loop so that a particular sequence can cycle several times before moving on to the remaining code.
  3. A means, if possible, to fade my El wire from dark to light and back to dark over a set time.

I am using the EL sequencer from sprakfun http://www.sparkfun.com/products/9203.
Also here is a sample of the code that I’ve written up.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

// turn a given EL wire segment on or off. 'num' is between 0 and 7, corresponding
// to EL segments 'A' through 'H'.  if 'value' is true, the segment will be lit.
// if value is false, the segment will be dark.
void elSegment(byte num, boolean value) {
  digitalWrite(num + 2, value ? HIGH : LOW);
}

void setup() {
  // set up the EL wire segment pins
  byte i;
  for (i = 2; i < 10; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
}

// control how long each segment is turned on by changing the delay
// delete the code that is not being used EL wire segments 
void loop() {
  elSegment(3, true);
  delay(2000);
  elSegment(3, false);
  
  
  elSegment(1, true);
  delay(2000);
  elSegment(1, false);
  
  
  elSegment(2, true);
  delay(2000);
  elSegment(2, false);

  
  elSegment(0, true);
  delay(2000);
  elSegment(0, false);

}

instead of having to write out elSegment(int,value) for each one.

The trick is to use an array to hold say the pin number. Then you use a for loop and the index variable of the loop becomes the index of the array to give you the pin you want to address.

A way to include a set number of loops

Hint you can nest loops within loops.

A means, if possible, to fade my El wire

I don't think this is possible, sorry.

Change your function elSegment to be:

void elSegment(byte num) {
  digitalWrite(num + 2,  HIGH );
  delay(2000);
  digitalWrite(num + 2, LOW);
}

then your main loop becomes:

void loop() {
  elSegment(3);
  elSegment(1);
  elSegment(2);
  elSegment(0);

}

you could take it one step further and do:

void loop()
{
  int elements[]={3,1,2,0};
  int i;
  for(i=0; i<4; i++)
 {
    elSegment(i);
 }
}

That’s more maintainable code because you can change the order by just editing the array, but it is less simple so I’m not sure it gets you anything.

you mean

void loop()
{
  int elements[]={3,1,2,0};
  int i;
  for(i=0; i<4; i++)
 {
    elSegment([color=red]elements[/color][i]);
 }
}

Perhaps simplifying code was the wrong way to put it as I am more interested in eliminating excess crap to make it more elegant and flow smoother without having to write long amounts of code. With your advice and a few ideas from others I’ve managed to get my on/off defined and a while loop set inside for a set number of times.

Currently I am working on writing code to fade something in as to fade it out would be simple enough to write after I solve this. Here’s what I got so far but isn’t working as I expected.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define pulse_width 3

// turn a given EL wire segment on or off. 'num' is between 0 and 7, corresponding
// to EL segments 'A' through 'H'.  if 'value' is true, the segment will be lit.
// if value is false, the segment will be dark.
void On(byte num) {
  digitalWrite(num + 2, HIGH);
}// turns the wire of num on
void Off(byte num) {
  digitalWrite(num +2, LOW);
}// turns the wire of num off

void fade_in(byte num) {
  for(int brightness=0; brightness<=pulse_width; brightness++){
    for(int duration=0; duration<1; duration++){
      On(byte num);
      delay(brightness);
      Off(byte num);
      delay(pulse_width-brightness);
    }
  }
  On(byte num)
}

I’m trying to get it so I can set which wire will fade in individually instead of all of them, but it appears I’m having an issue with the code for fade_in and am looking for some more help in solving it.

Also I am wondering if there is a way to have something happening repeatedly over the entire set of the code without restricting it to a while loop. For example I want wires 0-6 to do a variety of things but at the same time all of that is going on I’d like wire 7 to flash over the entire length of the code every 2 seconds without having to actually write it in every 2 seconds.

I am going through the references and examples but it is slow going.

I think what you want is pulse-width-modulation. See: http://www.arduino.cc/en/Tutorial/PWM

I’ve got the fading problem worked out. I’m still not finding anything on how to run a certain set of code continuously while another set runs along side it. What I’m trying to do is have 0-6 flash in a varied pattern that I’ve made but have 7 flash on/off in a continuous pattern at the same time without having to write it in when ever it needs to happen.

You use a state machine to do this. Look at the example blink without delay.