Format Multiple Commands

Hello...

I am working through understanding the programming language and had a question regarding proper formatting. I am working with LED's and have ran into a problem and the solution has me stumped. I am using the 'examples' as a learning tool.

Problem:

I have 4 LED's programmed to fade sequentially... the code functions fine.
I have 2 LED's that I would like to blink alternately.

After I assigned the 'int' of the LED's and label them as 'OUTPUT'... I write the fade code within the { }

When doing a separate command, such as a 'blink', where is that code properly placed? If I place the blink code inside the { } of the fade code... I get an erratic response. If I place the blink code in it's own { } after the fade code, I get a syntax error when verifying the code.

Yes... a very 'newb' question but am trying to learn it....

Thanks

SteffinT:
I have 4 LED's programmed to fade sequentially... the code functions fine.
I have 2 LED's that I would like to blink alternately.

After I assigned the 'int' of the LED's and label them as 'OUTPUT'... I write the fade code within the { }

When doing a separate command, such as a 'blink', where is that code properly placed? If I place the blink code inside the { } of the fade code... I get an erratic response. If I place the blink code in it's own { } after the fade code, I get a syntax error when verifying the code.

Hmm.. the code appears to be blank. Please post it (in code tags, please).

I am attempting to have 4 LED's fade sequentially and 2 LED's blink alternately. The code below reflects only 1 LED since I am simply trying to understand how things get formatted when you want to do two separate things. The 'code' below is trying to combine the fade code from a website and a blink code from the examples...

/*
Modified code from http://nothingtosomethingtutorials.blogspot.com
This is my attempt to copy an illusion of the leds fading smoothly to the next one...
Also, how to combine different code commands such as fade and blink in single structure
*/
int led=2    //this is part of the blink example I am attempting to combine
int thr=0;   // represents brightness for pin,
int fiv=75;  // each integer's name is the first three letters of the pin
int six=150; // it represents of the pin it represents this is just to keep
int nin=255; // track it represents

int c1=5;  //c represents change, each pin gets its own change
int c2=5;  // so it wont interfere with any other pin
int c3=5;
int c4=5;

void setup(){

pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(2,OUTPUT); //this is part of the blink example I am attempting to combine

}
void loop(){

analogWrite(3,thr);
thr=thr+c1;
analogWrite(5,fiv);
fiv=fiv-c2;
analogWrite(6,six);
six=six-c3;
analogWrite(9,nin);
nin=nin-c4;


if(thr==0 || thr==255){
c1=-c1;
}

if(fiv==0 || fiv==255){
c2=-c2;
 }

if(six==0 || six==255){
c3=-c3;
 }

if(nin==0 || nin==255){
c4=-c4;
}

delay(10);
}

/*
Not sure where this code would be placed
/*

 {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
int led=2    //this is part of the blink example I am attempting to combine

Meaningful names are very important. Is this supposed to be a ledPin or an ledState or an ledIndex? There is no way, from this poor name, to tell. The context where the variable is used provides clues, but it should be the other way around. The name should confirm that the context is correct. The context should not have to provide confirmation that the declaration is correct.

It isn't, really, if that is a pin number. No Arduino has enough pins that an int needs to be used to hold the pin number. Pin numbers should not change throughout the code. The way to assure that that doesn't happen is to make the variable not variable, by using the const keyword.

const byte ledPin = 2;

Note the semicolon on the end, too.

int thr=0;   // represents brightness for pin,
int fiv=75;  // each integer's name is the first three letters of the pin
int six=150; // it represents of the pin it represents this is just to keep
int nin=255; // track it represents

Now this just looks bizarre. Use names that make sense, not some abbreviation that makes for constant length names that don't mean anything.

/*
Not sure where this code would be placed
/*

All code must be in a function. Since you only have two functions, setup() that gets called once and loop() that gets called over and over, you really only have once choice.

Of course, that code is going to seriously interfere with the fading.

The blink without delay example bears looking at. Actually, more than looking at. You need to understand and embrace the methodology it illustrates, and forget that the delay() function exists. Don't lean on crutches you don't need.

Thank you for your time, did not realize this is a subject that has been beat to death - multiple times. After some additional reading, I understand now that the delay is something to steer clear from.

After a bit more tinkering, I modified a snippet of code I found to suit my needs, it appears to accomplish the sequential pattern I need.

Now... as far as a 'blink without delay' function, where does that code get placed? Is it after the last AnalogWrite command and before the final } or after the final }?

Thanks

int value, value2, value3, value4;// sets the interval of each led to perform sequential pattern
int ledpin1 = 3;                  // light connected to digital pin 3
int ledpin2 = 5;                  // light connected to digital pin 5
int ledpin3 = 6;                  // light connected to digital pin 6
int ledpin4 = 9;                  // light connected to digital pin 9
long time=0;

int period = 2000;
int displace = 500;
int displace2 = 1000;
int displace3 = 1500;


void setup() 
{ 
  // nothing for setup 
} 

void loop() 
{ 
  time = millis();
  //sets each value of lights
  value = 128+127*cos(2*PI/period*time);
  value2 = 128+127*cos(2*PI/period*(displace-time));
  value3 = 128+127*cos(2*PI/period*(displace2-time));
  value4 = 128+127*cos(2*PI/period*(displace3-time));
  

  analogWrite(ledpin1, value);           // sets the value of the fade from 0 to 255 
  analogWrite(ledpin2, value2);          // sets the value of the fade from 0 to 255
  analogWrite(ledpin3, value3);          // sets the value of the fade from 0 to 255
  analogWrite(ledpin4, value4);          // sets the value of the fade from 0 to 255

}

Problem resolved for 7 LEDs... to sequentially fade (scanner effect) four LED's and to have an additional 2 LED's blink alternately with 1 LED - all within the same sketch without 'issues'....

In the end, came up with this...

int value, value2, value3, value4;// sets the interval of each led to perform sequential pattern
int ledpin1 = 3;                  // light connected to digital pin 3, first light in sequential
int ledpin2 = 5;                  // light connected to digital pin 5, second light in sequential
int ledpin3 = 6;                  // light connected to digital pin 6, third light in sequential
int ledpin4 = 9;                  // light connected to digital pin 9, fourth light in sequential
const int ledPin5 = 7;            // light connected to digital pin 7, part of blinking without delay, alternates with pin 12
const int ledPin6 = 8;            // light connected to digital pin 8, part of blinking without delay, alternates with pin 12
const int ledPin7 = 12;           // light connected to digital pin 12, part of blinking without delay, alternates with pins 7 and 8

// this code controls the blinking without delay finction
int ledState = LOW;             //sets initial state of leds when unit is powered
int ledState2 = HIGH;           //sets the alternate led if you want an alternating blink
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)
//this is the end of the blinking without delay function

//this code is for the fading leds
long time=0;

int period = 2000;
int displace = 500;
int displace2 = 1000;
int displace3 = 1500;
//if adding more pwm lights, add additional displace in time sequence


void setup() 
{ 
//defines the blinking lights are an output
 pinMode(ledPin5, OUTPUT);
 pinMode(ledPin6, OUTPUT);
 pinMode(ledPin7, OUTPUT); 
} 

void loop() 
{ 
  //this is for the fading sequential leds
  time = millis();
  //sets each value of lights
  value = 128+127*cos(2*PI/period*time);
  value2 = 128+127*cos(2*PI/period*(displace-time));
  value3 = 128+127*cos(2*PI/period*(displace2-time));
  value4 = 128+127*cos(2*PI/period*(displace3-time));
  

  analogWrite(ledpin1, value);           // sets the value of the fade from 0 to 255 
  analogWrite(ledpin2, value2);          // sets the value of the fade from 0 to 255
  analogWrite(ledpin3, value3);          // sets the value of the fade from 0 to 255
  analogWrite(ledpin4, value4);          // sets the value of the fade from 0 to 255
  //end of code for sequential fading
  
  //this snippet of code controls the blinking without delay
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
      
    //this controls the alternate state of the opposing leds:
    if (ledState2 == HIGH)
      ledState2 = LOW;
    else
      ledState2 = HIGH;

    // set the LED with the ledState of the variable, basically where you want the pattern to start:
    digitalWrite(ledPin5, ledState);
    digitalWrite(ledPin6, ledState);
    digitalWrite(ledPin7, ledState2);
    

}
}

Whenever you see variable names with suffixes

int ledpin1 = 3;                  // light connected to digital pin 3, first light in sequential
int ledpin2 = 5;                  // light connected to digital pin 5, second light in sequential
int ledpin3 = 6;                  // light connected to digital pin 6, third light in sequential
int ledpin4 = 9;

, start thinking "array"