Pages: [1]   Go Down
Author Topic: passing parameters  (Read 540 times)
0 Members and 1 Guest are viewing this topic.
Riverside, ca
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The following code does not pass parameters to loop in getpulseStep.  All it does is print the number 5 and "pulseStep".


int rS;
int pD;
int k;
#define motorStep 11
#define runStop    6
#define  pedalDown  5
void setup(){
Serial.begin (9600);
pinMode (motorStep, OUTPUT);
pinMode (runStop, INPUT);
pinMode (pedalDown, INPUT);
}
void loop(){
  rS = digitalRead (runStop);
  pD = digitalRead (pedalDown); 

 while (rS == LOW){
      rS = digitalRead (runStop);
     pD = digitalRead (pedalDown);   
   while (pD == HIGH){
       pD = digitalRead (pedalDown);   
     Serial.println (pD);
      return;
   }
    int i = 5;
    k=getpulseStep(i);
   Serial.println (k);
}
}
   int getpulseStep(int c){           
   Serial.println ("pulseStep");
     for (int C=c ; C > 0; C--){
     Serial.println (C);
     return (C) ; 
   } 
 }           
 
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int C=c ; C > 0; C--){
     Serial.println (C);
     return (C) ;
   } 

It does pass parameters.

But you just throw them away.

This loop is fundamentally flawed:

1. Loop for c to 0 times:
1.1 Print C
1.2 return C
2. Never gets here - it just returned.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Riverside, ca
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  The intent is to pass the parameter 5 to getpulseStep and have the for l.oop execute it 5 times. The program runs on the Arduino simulator but not on the actualcard. Any suggestions as how implement it correctly?
 
  int i = 5;
    k=getpulseStep(i);
   Serial.println (k);
}
}
   int getpulseStep(int c){           
   Serial.println ("pulseStep");
     for (int C=c ; C > 0; C--){
     Serial.println (C);
     return (C) ; 
   } 
 }           
 
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can implement it correctly by not aborting your for loop partway through the first iteration with that return(C) line.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Riverside, ca
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, that fixed the problem but how do I return the value of "C" if it is outside the for loop?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25876
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but how do I return the value of "C" if it is outside the for loop?

But if you return it, you have to exit the loop.
What do you want to do?
Logged

"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.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The program runs on the Arduino simulator but not on the actualcard.

Oh? Strange.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you even want to return C?  It will always be 0.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: