calling functions with input pins

Hello again everyone, I have a program with three functions that I wanna call based on a digital input from a digitalRead. I have the program compiling fine and nothing turns on when i start the program. however, I cant get anything to turn on when i apply 5v to my input pins. any help on this would be greatly appreciated. First off I need to know if this can work the way I am doing it or if I would have to use an interupt. I dont think I need one, but i'm not sure. Then I would like to know if my sytax is correct. Also do i need an external power source to test this function, and how much resistance is needed, if any, do I need? My thought is it will only pull the current it needs so I shouldn't need a current limiting resistor. Also with a digitalRead function should I use pinMode to initialize these three pins as an input?

void loopControlFunction(int motorControl, int lightControl, int sensorControl){
  
  digitalRead(motorControl);
  digitalRead(lightControl);
  digitalRead(sensorControl);
     if(motorControl == HIGH){
       motorControlLoop();
     }
     if(lightControl == HIGH){
       lightControlLoop();
     }
     if(sensorControl == HIGH){
       sensorControlLoop();
     }
}
  digitalRead(motorControl);
  digitalRead(lightControl);
  digitalRead(sensorControl);

Read the state of the three pins and throw away the results

     if(motorControl == HIGH){
       motorControlLoop();
     }
     if(lightControl == HIGH){
       lightControlLoop();
     }
     if(sensorControl == HIGH){
       sensorControlLoop();
     }
}

Then test if the pin number is equal to HIGH.

You actually have to compare the result of the digitalRead() with HIGH if you want to do anything meaningful. This is another example of why its good to differentiate pin variables and state variables when naming them.

isnt that what im doing with my if statement?

pwrbildr: Isnt that what im doing with my if statement?

No. If you want to test whether the result from a call to digitalRead() is high or low, you have to call digitalRead() and test whether [u]the result[/u] is high or low.

if(digitalRead(lightControl) == HIGH)

Isnt that what im doing with my if statement?

Nope! ;)

if(motorControl == HIGH)

I assume the variable (or constant) named motorControl is a pin number. Its not the high/low value returned when you read that pin. If that variable (pin number) is not equal to zero, it will be will always be HIGH and your if condition will always be true.

You can think of the pin number (motorControl) as an address. If you have the answer to our math homework, I can't write you house number as the answer... I have to go to your address to get the answer.

You can read the motorControl pin and assign the result to a boolean variable, and then check to see if that boolean varaible is HIGH.

Or, you can do this:

if(digitalRead(motorControl)) == HIGH)

Do i need to set the pins as output or input if i use digital read/write?

You read inputs and you write outputs... Information comes-into inputs and goes-out of outputs.

(There is a weird exception where you write to an input pin to enable the internal pull-up.)

ok so i fixed that but now if i “shut off” the code or remove the input it gets stuck in the middle. I want to write an if statement that if pin reads LOW to reset. How would I do that? Also it seems to only use one pin to turn all of the functions on. How would I go about fixing that. Here is all my code.

int motorPinA = 12, brakePinA = 9, motorPinB = 13, brakePinB = 8;
int redPin = A8, greenPin = A9, bluePin = A10;
int musicPin = 24;
int i = 1;
int j = 0;
int brakeState = LOW;
int motorControl = 25, lightControl = 26, sensorControl = 27;

long lightInterval = 5000;
long interval = 1000;
long previousMillis = 0;
long lightPrevMillis = 0;

unsigned int matBits;
unsigned int lastBits = 0xFFFF; // 0x denotes hexadecimal notation. 0xFFFF is all bits set

int alarmPin = 21;
int alarmCount = 0;




void setup() {
  
  //Setup Channel A
  pinMode(motorPinA, OUTPUT); //Initiates Motor Channel A pin
  pinMode(brakePinA, OUTPUT); //Initiates Brake Channel A pin
  pinMode(motorPinB, OUTPUT); //Initiates Motor Channel B pin
  pinMode(brakePinB, OUTPUT); //Initiates Brake Channel B pin
  
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
  pinMode (21, OUTPUT);
}

void loop(){
  loopControlFunction(motorControl, lightControl, sensorControl);
}


void motorControlLoop(){  
      unsigned long currentMillis = millis();
      digitalWrite (motorPinA, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3,255);    //Spins the motor on Channel A at half speed
 
      if(currentMillis - previousMillis >= interval){
        previousMillis = currentMillis;
      
      if(brakeState == LOW) brakeState = HIGH;
      else brakeState = LOW;
      
      digitalWrite(brakePinA, brakeState);
      }      
}
void lightControlLoop(){
 
    digitalWrite(musicPin, HIGH);
    unsigned long currentMillis = millis();
    
     if (currentMillis - lightPrevMillis >= lightInterval){
       i++;
      lightPrevMillis = currentMillis;   
    }
  
  
    if(i==1) setColor(255, 0, 0);      // Red
    if(i==2) setColor(0, 255, 0);      // Gree
    if(i==3) setColor(0, 0, 255);      // Blue
    if(i==4) setColor(255, 255, 0);    // Red + Green
    if(i==5) setColor(0, 255, 255);    // Green + Blue
    if(i==6) setColor(255, 0, 255);    // Red + Blue
    if(i==7) i = 1;
    
   //Motor B forward @ full speed
    digitalWrite(motorPinB, HIGH);  //Establishes forward direction of Channel B
    digitalWrite(brakePinB, LOW);    //Disengage the Brake for Channel B
    analogWrite(11, 255);    //Spins the motor on Channel B at full speed
}
void setColor(int red, int green, int blue){
    analogWrite(redPin, 255-red);
    analogWrite(greenPin, 255-green);
    analogWrite(bluePin, 255-blue);
  }
void sensorControlLoop(){
  
  for ( byte k = 31; k < 47; i++ ) 
  {
    bitWrite( matBits, k - 31, digitalRead( k ) );
  }

  if ( matBits != lastBits )
  {
    ++alarmCount;
    if(alarmCount > 25000){
      digitalWrite(alarmPin, HIGH);
  }
  else alarmCount = 0;

  lastBits = matBits;
} // end of loop()
  
}

void loopControlFunction(int motorControl, int lightControl, int sensorControl){
  

     if(digitalRead(motorControl) == HIGH){
       motorControlLoop();
     }
     else if(digitalRead(motorControl) == LOW){
       asm volatile("jmp 0");
     }
     if(digitalRead(lightControl) == HIGH){
       lightControlLoop();
     }
     if(digitalRead(sensorControl) == HIGH){
       sensorControlLoop();
     }
     else return;
     
}

I put the ask volatile piece in there cause I thought it might reset it but it didn’t

You need to stop using pronouns in inapprorpirate places to make your explanations less confusing:

pwrbildr: it

what?

middle

..of what?

pin

what pin?

reset

reset what?

to turn all of the functions on.

functions don't have on and off states. You mean all of the functions are called?

How would I go about fixing that

to do what?

pwrbildr:

void sensorControlLoop(){

for ( byte k = 31; k < 47; i++ )

Why are you incrementing i when k is the loop counter?

      ++alarmCount;

Try

alarmCount++;

Henry_Best:

      ++alarmCount;

Try

alarmCount++;

There's lots wrong with the code posted, but there's nothing wrong with this part - if you don't use the value, it doesn't matter whether you use a pre-increment or post-increment operator.

PeterH:

Henry_Best:

      ++alarmCount;

Try

alarmCount++;

There's lots wrong with the code posted, but there's nothing wrong with this part - if you don't use the value, it doesn't matter whether you use a pre-increment or post-increment operator.

Thanks Peter. I've never seen that construct before.

OK so I fixed the i incrementation. Sorry about all the lousy explanation earlier. My problem is if i send the motorcontrol pin, it controls the motorcontrol loop activating the function, it also starts the light control function. I am trying to activate each individual function with a separate pin. However if I do activate them they wont shut off either. In addition to them all activating once they do activate, and i remove the high signal to the control pin, I can’t get the functions to stop entirely. They remain stuck at the point they were in when i removed the input signal. Any help would be greatly appreciated.

int motorPinA = 12, brakePinA = 9, motorPinB = 13, brakePinB = 8;
int redPin = A8, greenPin = A9, bluePin = A10;
int musicPin = 24;
int i = 1;
int j = 0;
int brakeState = LOW;
int motorControl = 25, lightControl = 26, sensorControl = 27;

long lightInterval = 5000;
long interval = 1000;
long previousMillis = 0;
long lightPrevMillis = 0;

unsigned int matBits;
unsigned int lastBits = 0xFFFF; // 0x denotes hexadecimal notation. 0xFFFF is all bits set

int alarmPin = 21;
int alarmCount = 0;




void setup() {
  
  //Setup Channel A
  pinMode(motorPinA, OUTPUT); //Initiates Motor Channel A pin
  pinMode(brakePinA, OUTPUT); //Initiates Brake Channel A pin
  pinMode(motorPinB, OUTPUT); //Initiates Motor Channel B pin
  pinMode(brakePinB, OUTPUT); //Initiates Brake Channel B pin
  
}

void loop(){
  loopControlFunction(motorControl, lightControl, sensorControl);
}


void motorControlLoop(){  
      unsigned long currentMillis = millis();
      digitalWrite (motorPinA, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3,255);    //Spins the motor on Channel A at half speed
 
      if(currentMillis - previousMillis >= interval){
        previousMillis = currentMillis;
      
      if(brakeState == LOW) brakeState = HIGH;
      else brakeState = LOW;
      
      digitalWrite(brakePinA, brakeState);
      }      
}
void lightControlLoop(){
 
    digitalWrite(musicPin, HIGH);
    unsigned long currentMillis = millis();
    
     if (currentMillis - lightPrevMillis >= lightInterval){
       i++;
      lightPrevMillis = currentMillis;   
    }
  
  
    if(i==1) setColor(255, 0, 0);      // Red
    if(i==2) setColor(0, 255, 0);      // Gree
    if(i==3) setColor(0, 0, 255);      // Blue
    if(i==4) setColor(255, 255, 0);    // Red + Green
    if(i==5) setColor(0, 255, 255);    // Green + Blue
    if(i==6) setColor(255, 0, 255);    // Red + Blue
    if(i==7) i = 1;
    
   //Motor B forward @ full speed
    digitalWrite(motorPinB, HIGH);  //Establishes forward direction of Channel B
    digitalWrite(brakePinB, LOW);    //Disengage the Brake for Channel B
    analogWrite(11, 255);    //Spins the motor on Channel B at full speed
}
void setColor(int red, int green, int blue){
    analogWrite(redPin, 255-red);
    analogWrite(greenPin, 255-green);
    analogWrite(bluePin, 255-blue);
  }
void sensorControlLoop(){
  
  for ( byte k = 31; k < 47; k++ ) 
  {
    bitWrite( matBits, k - 31, digitalRead( k ) );
  }

  if ( matBits != lastBits )
  {
    ++alarmCount;
    if(alarmCount > 25000){
      digitalWrite(alarmPin, HIGH);
  }
  else alarmCount = 0;

  lastBits = matBits;
} // end of loop()
  
}

void loopControlFunction(int motorControl, int lightControl, int sensorControl){
  

     if(digitalRead(motorControl) == HIGH){
       motorControlLoop();
     }
     if(digitalRead(lightControl) == HIGH){
       lightControlLoop();
     }
     if(digitalRead(sensorControl) == HIGH){
       sensorControlLoop();
     }
     else return;
     
}

pwrbildr: OK so I fixed the i incrementation. Sorry about all the lousy explanation earlier. My problem is if i send the motorcontrol pin, it controls the motorcontrol loop activating the function, it also starts the light control function. I am trying to activate each individual function with a separate pin. However if I do activate them they wont shut off either. In addition to them all activating once they do activate, and i remove the high signal to the control pin, I can't get the functions to stop entirely. They remain stuck at the point they were in when i removed the input signal. Any help would be greatly appreciated.

I read that several times and found it very hard to figure out what you mean.

You are passing three pin numbers into loopControlFunction(). Given that you only pass in one set of argument values, and the values are global constants (with the same name as the function arguments) is there any point to these arguments? You aren't abstracting or parameterising anything, you are just obscuring the use of global data.

With the program structure you have used, your three main control functions will only be called when the corresponding input is active. If the input goes inactive, they will cease to be called. In that sense they do 'stop entirely'. Perhaps you want to switch off or disable the things that these outputs are controlling. In that case you would need to explicitly do that when the input was low. One way to implement that is to throw away the 'loopControlFunction()' and simply call the three control functions in loop(). Each control function can read the state of the input that enables it, and take the appropriate action.

Your global counters i and j are abysmally named and asking for trouble, because these names are commonly used as temporary local counters. You should really consider giving them meaningful names so that it's clear what they represent, and also to avoid potential conflict with any temporary local counter variables that might get added in future.

I read that several times and found it very hard to figure out what you mean.

You are passing three pin numbers into loopControlFunction(). Given that you only pass in one set of argument values, and the values are global constants (with the same name as the function arguments) is there any point to these arguments? You aren’t abstracting or parameterising anything, you are just obscuring the use of global data.

With the program structure you have used, your three main control functions will only be called when the corresponding input is active. If the input goes inactive, they will cease to be called. In that sense they do ‘stop entirely’. Perhaps you want to switch off or disable the things that these outputs are controlling. In that case you would need to explicitly do that when the input was low. One way to implement that is to throw away the ‘loopControlFunction()’ and simply call the three control functions in loop(). Each control function can read the state of the input that enables it, and take the appropriate action.

Your global counters i and j are abysmally named and asking for trouble, because these names are commonly used as temporary local counters. You should really consider giving them meaningful names so that it’s clear what they represent, and also to avoid potential conflict with any temporary local counter variables that might get added in future.

what i want to do is find a way to call each function individually with an input, like if(digitalRead(motorPin) == HIGH) than the motorControlFunction() is active. That way I can use a switch to start/stop individual functions. That way i can control the features I want to run. For instance if I want the Lights on but not the motors, all I have to do is flip a switch. However, I will have a switch that will activate some features like motor and lights, but not the sensor loop. With my mega 2560 I have several digital pins I can use. So I thought I could read the inputs off of each pin and if they are high, assign a particular response within the code. I dont really know how to explain it. I changed the global counters. Turns out I wasnt even using j.

int motorPinA = 12, brakePinA = 9, motorPinB = 13, brakePinB = 8;
int redPin = A8, greenPin = A9, bluePin = A10;
int musicPin = 24;
int lightCount = 1;
int brakeState = LOW;
int motorPin = 25, lightPin = 26, sensorPin = 27;

long lightInterval = 5000;
long interval = 1000;
long previousMillis = 0;
long lightPrevMillis = 0;

unsigned int matBits;
unsigned int lastBits = 0xFFFF; // 0x denotes hexadecimal notation. 0xFFFF is all bits set

int alarmPin = 21;
int alarmCount = 0;
int alarmState = LOW;




void setup() {
  
  //Setup Channel A
  pinMode(motorPinA, OUTPUT); //Initiates Motor Channel A pin
  pinMode(brakePinA, OUTPUT); //Initiates Brake Channel A pin
  pinMode(motorPinB, OUTPUT); //Initiates Motor Channel B pin
  pinMode(brakePinB, OUTPUT); //Initiates Brake Channel B pin
  

  
  pinMode(31, INPUT);
  pinMode(32, INPUT);
  pinMode(33, INPUT);
  pinMode(34, INPUT);
  pinMode(35, INPUT);
  pinMode(36, INPUT);
  pinMode(37, INPUT);
  pinMode(38, INPUT);
  pinMode(39, INPUT);
  pinMode(40, INPUT);
  pinMode(41, INPUT);
  pinMode(42, INPUT);
  pinMode(43, INPUT);
  pinMode(44, INPUT);
  pinMode(45, INPUT);
  pinMode(47, INPUT);
  
}

void loop(){
  motorControlLoop();
  lightControlLoop();
  sensorControlLoop();
}


void motorControlLoop(){  
      unsigned long currentMillis = millis();
      digitalWrite (motorPinA, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3,255);    //Spins the motor on Channel A at half speed
 
      if(currentMillis - previousMillis >= interval){
        previousMillis = currentMillis;
      
      if(brakeState == LOW) brakeState = HIGH;
      else brakeState = LOW;
      
      digitalWrite(brakePinA, brakeState);
      }      
}
void lightControlLoop(){
 
    digitalWrite(musicPin, HIGH);
    unsigned long currentMillis = millis();
    
     if (currentMillis - lightPrevMillis >= lightInterval){
       lightCount++;
      lightPrevMillis = currentMillis;   
    }
  
  
    if(lightCount == 1) setColor(255, 0, 0);      // Red
    if(lightCount == 2) setColor(0, 255, 0);      // Gree
    if(lightCount == 3) setColor(0, 0, 255);      // Blue
    if(lightCount == 4) setColor(255, 255, 0);    // Red + Green
    if(lightCount == 5) setColor(0, 255, 255);    // Green + Blue
    if(lightCount == 6) setColor(255, 0, 255);    // Red + Blue
    if(lightCount == 7) lightCount = 1;
    
   //Motor B forward @ full speed
    digitalWrite(motorPinB, HIGH);  //Establishes forward direction of Channel B
    digitalWrite(brakePinB, LOW);    //Disengage the Brake for Channel B
    analogWrite(11, 255);    //Spins the motor on Channel B at full speed
}
void setColor(int red, int green, int blue){
    analogWrite(redPin, 255-red);
    analogWrite(greenPin, 255-green);
    analogWrite(bluePin, 255-blue);
  }
void sensorControlLoop(){
  
  for ( byte k = 31; k < 47; k++ ) 
  {
    bitWrite( matBits, k - 31, digitalRead( k ) );
  }

  if ( matBits != lastBits )
  {
    ++alarmCount;
    if(alarmCount > 25000){
      if(alarmState == LOW) alarmState = HIGH;
      else alarmState = LOW;
      digitalWrite(alarmPin, alarmState);
  }
  else alarmCount = 0;

  lastBits = matBits;
} // end of loop()
  
}

So in the loop control I wanna be able to trigger each function individually, and in some instances two or more, based on the input of a pin going high or low. Does that make sense?
This is the revised code. How can I make the loop function be able to call individual functions with a high signal, and then stop that function with a low signal?

pwrbildr: How can I make the loop function be able to call individual functions with a high signal, and then stop that function with a low signal?

What do you mean by 'stop' in this context?

For example the motorControlLoop() is used to turn cycle a vibrating motor on and off. Also the lightControlLoop() is used to control LEDs. If i apply a five volt signal to either pin that I am using to control the three functions, everything turns runs fine. However, when I remove the voltage the motors themselves will either continue to run or the brake will stay engaged. The LEDs for the lightControlLoop() will continue to stay on and just pause at one color. I need to be able to "shut off" these devices by not calling the function and reset the arduino so it waits for either control pin to go high again before it calls the functions again.

pwrbildr: I need to be able to "shut off" these devices by not calling the function and reset the arduino so it waits for either control pin to go high again before it calls the functions again.

Just not calling the control functions won't achieve that. You need to execute code to reset each device back to it's "idle" state (stop motors, turn LEDs off etc). This could be as simple as an if/else construct that either calls the control function, or calls some other function to do the reset.

I know you cant "shut off" a function, but how do I reset my code to get it back. I am using an if statement to turn on functions and that works fine. However, I dont know how to get the arduino to reset without pressing the actual reset button.

pwrbildr: How do I reset my code to get it back.

You're thinking about this problem backwards.

What do you need to do when each input is inactive? Be specific, and describe the behaviour you need in terms of the Arduino outputs. Then write a function that implements that behaviour, and call it when the input is inactive. If you want the LEDs to turn off, write some code to turn them off. If you want the motor to stop, write some code to stop it. And so on.