Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpSyntax › Loop through outputs problem

Page Index Toggle Pages: 1
Loop through outputs problem (Read 6545 times)
05/07/08 at 04:26:51

William   Offline
YaBB Newbies
I Love YaBB 2!

Posts: 2
Heya folks

Using my actionscript knowledge I am trying to randomly switch off and on a number of outputs. Heres the code:

int LEDPin6 = 23;
int LEDPin5 = 22;
int LEDPin4 = 21;
int LEDPin3 = 20;
int LEDPin2 = 19;
int LEDPin1 = 18;
void setup(){
  pinMode(LEDPin1, OUTPUT);
  pinMode(LEDPin2, OUTPUT);
  pinMode(LEDPin3, OUTPUT);
  pinMode(LEDPin4, OUTPUT);
  pinMode(LEDPin5, OUTPUT);
  pinMode(LEDPin6, OUTPUT);

void loop(){
  int i;
  for(i=0; i<=6; i++) {
    int x;
    int y;
    x = random(5000);
    y = random(5000);
    digitalWrite (LEDPin+i, HIGH);
    delay (x);
    digitalWrite (LEDPin+i, LOW);
    delay (y);
    if (i>6){

but does not work, do I have to code this the long way instead or is there something basic I am missing. Obviously this is written in a procedural manner, should this be written in an oop manner?
IP Logged
Reply #1 - 05/07/08 at 06:11:25

barragan   Offline
YaBB Administrator

Posts: 939
yes, this is wrong the variable LEDPin doesn´t exist,
what you could do is this:

int LEDPins[] = {10, 19, 20, 21, 22, 23};

for(int i = 0; i<6; i++) {
  digitalWrite(LEDPins[i], HIGH);
  digitalWrite(LEDPins[i], LOW);

I guess this might help you to understand how variables work and how to iterate through the pins.
IP Logged
Reply #2 - 05/07/08 at 10:13:42

William   Offline
YaBB Newbies
I Love YaBB 2!

Posts: 2
Yes! quite so, thank you. I hope this helps others out there.
IP Logged
Reply #3 - 05/08/08 at 06:23:42

Titus   Offline
Full Member
Wiring Soccer Robot!

Posts: 176
Dear  William,

An int uses way more space then other variables.
If you are using pins, or values that won't change during your program, please use:

# define LEDpin 10
# define LEDpin 11

This uses less memory, and the values can never be changed accedentely


Check www.TeamVossius.nl for our Autonomous soccer robots!
IP Logged
Page Index Toggle Pages: 1