Go Down

Topic: help with function (Read 497 times) previous topic - next topic

farsajik

hi.I want to make KNIGHT RIDER.i write LedOff() function to turn off leds.it work well.but i cant write LedsOn() function please help me.I want something like this
LedsOn(2);  led 2 turns on
LedsOn(2,3); led 2 and led 3 turns on
LedsOn(2,3,4,5,6,7);  led 2,3,4,5,6,7 turns on

this code work fine
Code: [Select]

int pinArray[] = {6, 10, 13,};

void setup(){
  for (int count=0;count<3;count++) {
    pinMode(pinArray[count], OUTPUT);
  }
}

void loop() {
  digitalWrite(6, HIGH);
  delay(1000);
  LedsOff();
 
  digitalWrite(6, HIGH);
  digitalWrite(10, HIGH);
  delay(1000);
  LedsOff();
 
  digitalWrite(6, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(13, HIGH);
  delay(1000);
  LedsOff();
 
  digitalWrite(13, HIGH);
  delay(1000);
  LedsOff();
 
  digitalWrite(10, HIGH);
  delay(1000);
  LedsOff();
 
  digitalWrite(6, HIGH);
  delay(1000);
  LedsOff();
 
}

void LedsOff(void)
{
  for (int count=0;count<3;count++) {
    digitalWrite(pinArray[count], LOW);
  }
}


but in this code i get error   INVALID CONVERSION FROM 'INT' to 'int*'
Code: [Select]
int pinArray[] = {6, 10, 13,};

void setup(){
  for (int count=0;count<3;count++) {
    pinMode(pinArray[count], OUTPUT);
  }
}

void loop() {
 
  LedsOn(6);
  delay(1000);
  LedsOff();
 
  LedsOn(6,10);
  delay(1000);
  LedsOff();
 
  LedsOn(6,10,13);
  delay(1000);
  LedsOff();
 
}

void LedsOff(void)
{
  for (int count=0;count<3;count++) {
    digitalWrite(pinArray[count], LOW);
  }
}

void LedsOn(int pinAray[3])
{
  for (int counts=0;counts<3;counts++) {
    digitalWrite(pinAray[counts], HIGH);
  }
}


AWOL

Code: [Select]
digitalWrite(pinAray[counts], HIGH);spelling?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

farsajik

I dont understand what you say.can you write correct function.thanks

AWOL

#3
Mar 03, 2013, 07:42 pm Last Edit: Mar 03, 2013, 07:45 pm by AWOL Reason: 1
Array != Aray
I don't understand why you pass one function an array pointer, but not to the other.
Code: [Select]
LedsOn(6,10,13); There are no function overloads visible. Did you post all your code?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

farsajik

#4
Mar 03, 2013, 07:49 pm Last Edit: Mar 03, 2013, 07:51 pm by farsajik Reason: 1
my LedsOn function is

Code: [Select]

void LedsOn(int pinAray[3])
{
 for (int counts=0;counts<3;counts++) {
   digitalWrite(pinAray[counts], HIGH);
 }
}

AWOL

Code: [Select]
LedsOn(6,10,13);so why call it like that, if there's no overload?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

farsajik

please write correct form .how it should be???

AWOL

Quote
how it should be?

I don't know - I don't understand the form of the call you're using.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Arrch

Code: [Select]

void LedsOn(int pinAray[3])


What you're saying here is When I call you, I'm going to send you an array of ints, which will be represented by the address of the first element.

Code: [Select]
LedsOn(6,10,13);

But when you call it, you're saying Here are three, independent int variables for you to use.

You need to package the values you want to send in an array, and send the array as the argument. Since it looks like you aren't going to be only sending three values, I would also pass it the number of pins you are sending it.

farsajik

how solve this problem?

Arrch


how solve this problem?


By doing what I said, pack the pins in an array and send it to the function.

AWOL

.and if the array is shorter than the function expects, don't forget to send the length.
Or use a bit mask.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

farsajik

i am new in arduino programming.can you show me some example how do it.thanks for anwering

Go Up