How to reset every pin when changing between functions?

I've been doing some code, I have four functions that I want to work when a condition is met, but I want to reset everything that is on the pins when I change between the. For example, if switch one is on and switch two is also on some leds turn on, and other actios perform too. If I change the condition and switch one is off and switch two is also off, I need LEDs to be off to perform other tasks but they keep on from the last function, so i want to reset every pin in the arduino to reset.

Here's my code:

//variables
int valorMap = 0;
int valorMapfoto = 0;
int valorFoto;
int valorPot = 0;
Servo servo_2;
int switch1;
int switch2;
int valorMotor;
int push1;
int push2;
int ledverde;
int ledroja;
int ledazul;
int pot;

//################PRIMERA FUNCION##################
//los dos switches estan apagados
int funcion1(){

//servo con potenciometro

valorPot = analogRead(A0);// valor del potenciometro lo lee del pin A0
servo_2.write(valorMap);//mueve el servo viertos grados
analogWrite(11, valorMotor);

//pushbutton1 enciende 2 led

if (push1 == LOW){//encender dos les con el primer psuhbutton
digitalWrite(13, HIGH);//encender led verde
digitalWrite(12,HIGH);//encender led amarillo
}
else {// apaga leds
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}

//pushbutton 2 enciende 2 led 

if (push2 == LOW){//encender dos les con el segundo psuhbutton
digitalWrite(10, HIGH);//encender led rojo
digitalWrite(9,HIGH);//encender led azul
}

else {//apaga leds
digitalWrite(10,LOW);
digitalWrite(9,LOW);
}

delay(10); // Esperar 10 ms

}

//################SEGUNDA FUNCION##################
//un switch encendido y el otro apagados
int funcion2(){

//apagar motor
digitalWrite(11,LOW);

//led verde con potencenciometro

valorPot = analogRead(A0);// valor del potenciometro lo lee del pin A0
ledverde = map(valorPot, 0, 1023, 0, 255);//va encendiendo el led verde
analogWrite(3,ledverde);//envia info

//led rgb rojo con fotoresistencia

analogWrite(6,valorMotor);//podemos reutilizar el valor motor para encender led rgb

//led azul con push button

if (push1 == LOW){//encender azul rgb les con el primer psuhbutton
digitalWrite(5, HIGH);//encender led azul
}
else {//apaga led azul rgb
digitalWrite(5,LOW);
}

//las cuatro leds deben estar encendidas

digitalWrite(13, HIGH);
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);

//si el pushbutton se presiona se apagan las led

if (push2 == LOW){
digitalWrite(13, LOW);
digitalWrite(12,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
}
delay(10); // Esperar 10 ms

}
//##################TERCERA FUNCION###################
//switch 1 apagado y switch 2 encendido
int funcion3(){

//apagar motor
digitalWrite(11,LOW);

//leer pines

int vfotof3 = map(valorFoto,0, 1023, 0, 255);//mapea cambios en fotoresistencia
int vfotof32 = map(valorFoto,1023,0,0,255);//mapea foto y cambia a rojo
int valorFotof2 = analogRead(A1);//leer la fotorresistencia

//si el push se presiona llevar a 0 grados

if (push1 == LOW){
servo_2.write(0);//mueve el servo viertos grados
}

//si el push se presiona llevar a 180 grados

if (push2 == LOW){
servo_2.write(180);//mueve el servo viertos grados
}

//controlar leds encendidos con potenciometro

valorPot = analogRead(A0);//leer el potenciometro

//encender leds en cascada
//si el pot menor encender led verde

if (valorPot < 256){
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
}
//encender led verde y amarilla
else if (valorPot < 512) {
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
}
//encender led verde,amarilla y roja
else if (valorPot < 768) {
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
}

//encender todas las leds

else if (valorPot < 1024) {
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
}

//fotocontrola la iluminacion de rgb 
//leer colores con analog read

analogWrite(3,vfotof3);//cambia a verde rgb
analogWrite(6,vfotof32);//cambia a rojo rgb
delay(10); // Esperar 10 ms

}

//######################CUARTA FUNCION#####################
//los dos switches encendidos
int funcion4(){

//apagar motor
digitalWrite(11,LOW);

//si el pushbutton esta presinado el potenciometro
//controla el led rojo rgb 

if (push1 == LOW){
valorPot = analogRead(A0);// valor del potenciometro lo lee del pin A0
ledroja = map(valorPot, 0, 1023, 0, 255);//va encendiendo el led verde
analogWrite(6,ledroja);//envia info
}
else{
push1 == HIGH;
}

//si el pushbutton2 se presiona 
//el potenciometro controla el led verdergb

if (push2 == LOW){
valorPot = analogRead(A0);// valor del potenciometro lo lee del pin A0
ledverde = map(valorPot, 0, 1023, 0, 255);//va encendiendo el led verde
analogWrite(3,ledverde);//envia info
}
else{
push1 == HIGH;
}

//si la foto esta a mas de 50% el pot controla azul rgb

int valorMapfoto2 = map(valorFoto, 54, 974, 0, 100);//hacer map de 0 a 100 del resitor

//si el foto es mayo de 50%, poten controla azul rgb

if (valorMapfoto2 >= 50){
valorPot = analogRead(A0);
ledazul = map(valorPot, 0, 1023, 0, 255);//va encendiendo el led verde
analogWrite(5,ledazul);//envia info
}

//si no es mayor, no lo controlo

if (valorMapfoto2 < 50 & (push1 == HIGH & push2 == HIGH)){
valorMapfoto2 < 50;
}

//si es menor de 50 y algun boton esta presionado apagar led

if (valorMapfoto2 < 50 & (push1 == LOW or push2 == LOW)){
digitalWrite(5,LOW);
digitalWrite(3,LOW);
digitalWrite(6,LOW);
}
delay(10); // Esperar 10 ms

}

//setup
void setup()
{

//definimos los pines(pullup messirve para los switches)
pinMode(A0, INPUT);
pinMode(A4, INPUT_PULLUP);
pinMode(A5, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
pinMode(11, OUTPUT);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9, OUTPUT);
servo_2.attach(2);
Serial.begin(9600);

}

void loop()
{

//Definir cada variable

switch1 = digitalRead(A2);
Serial.println(switch1);//switch 1 con pin A2

switch2 = digitalRead(A3);
Serial.println(switch2);//switch2 con pin A3

valorFoto = analogRead(A1);
//Serial.println(valorFoto);// fotoresistencia con pin A3

push1 = digitalRead(A4);//lee valores del primer pushbutton

push2 = digitalRead(A5);//lee valores del segundo pushbutton

//Funciones para mapaear posiciones y generar movimientos
valorMap = map(valorPot, 0, 1023, 0, 179);//mapea el ambio en el potencimetro
valorMotor = map(valorFoto,50, 1000, 0, 100);//mapea cambios en fotoresistencia

//funciones
if (switch1 == HIGH && switch2 == HIGH){
funcion1();
}

else if (switch1 == LOW && switch2 == HIGH){

funcion2();

}

else if (switch1 == HIGH && switch2 == LOW){
funcion3();
}

else if (switch1 == LOW && switch2 == LOW){
funcion4();
}
}

Hi @lpgp352

to post sketch, use tags </>

RV mineirin

1 Like

Read the forum guidelines.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

1 Like

www.arduino.cc/en/Reference/PortManipulation

for example you has green LEDs on pins (4, 5, 6, 7) and red on pins(8, 9,10, 11),
then for green
PORTD |= B11110000; to set on
PORTD &= ~(B11110000); to set off
for red
PORTB |= B00001111; to set on
PORTB &= ~(B00001111); to set off

enjoy & good luck

1 Like

You could write a function:

void AllOff()
{
  digitalWrite(11, LOW);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
}
3 Likes