Paso 4: Adición de salidas
Ahora que hemos probado diferentes entradas, podemos ver cómo salidas se pueden utilizar en las declaraciones de switch() así. Usaremos la MegaMoto para controlar un actuador para extender y retraer. Este ejemplo es similar al primer ejemplo, solo estamos utilizando retardos.
Ver el esquema y cargar el código. Ver la MegaMoto Instructable para más información detallada sobre el controlador del motor. Mientras el programa está funcionando, el motor va a mover y se puede ver el serial monitor para ver lo que está sucediendo.
Ya que estos son solo programas de prueba, puede dejar el tablero conectado a la computadora para que el Arduino puede recibir energía.
/* This code is to show how outputs are used in a switch statement. The code moves an actuator in and out at varying speeds, according to the sequence. Written by Progressive Automations Sept 21, 2015 This code is in the public domain */ const int enable = 8; const int PWMA = 11; const int PWMB = 3;//pins for the MegaMoto int programCount = 0;//variable to move through the program void setup() { Serial.begin(9600);// initialize serial communication: programCount = 0;//start at the beginning pinMode(enable, OUTPUT); pinMode(PWMA, OUTPUT); pinMode(PWMB, OUTPUT); }//end setup void loop() { switch (programCount) { case 0: digitalWrite(enable, HIGH);//enable the control board Serial.println("Motor moving forwards at full speed"); analogWrite(PWMA, 255); analogWrite(PWMB, 0);//set the speed of the actuator delay(5000);//Move for 5 seconds programCount = 1; break; case 1: Serial.println("Motor moving backwards at full speed"); analogWrite(PWMA, 0); analogWrite(PWMB, 255);//set the speed of the actuator delay(5000);//Move for 5 seconds programCount = 2; break; case 2: Serial.println("Motor moving forwards at half speed"); analogWrite(PWMA, 128); analogWrite(PWMB, 0);//set the speed of the actuator delay(3000);//Move for 3 seconds programCount = 3; break; case 3: Serial.println("Motor moving forwards at full speed"); analogWrite(PWMA, 255); analogWrite(PWMB, 0);//set the speed of the actuator delay(2000);//Move for 2 seconds programCount = 4; break; case 4: Serial.println("Motor moving backwards at full speed"); analogWrite(PWMA, 0); analogWrite(PWMB, 255);//set the speed of the actuator delay(2000);//Move for 2 seconds programCount = 5; break; case 5: Serial.println("Motor moving backwards at half speed"); analogWrite(PWMA, 0); analogWrite(PWMB, 128);//set the speed of the actuator delay(3000);//Move for 3 seconds programCount = 6; break; default: Serial.print("Movement complete"); while(1); //freeze the program here }//end switch }//end loop