MakerFaire Display Examples CODE
Jump to navigation
Jump to search
MakerFaire_Display_Examples_CODE
Example 1: 5 Buttons control 3 LEDS
/* YourDuinoStarter Example: Multiple Pushbuttons - WHAT IT DOES: Multi Buttons control multi LEDS - SEE the comments after "//" on each line below - CONNECTIONS: - - - V1.00 09/20/15 Questions: terry@yourduino.com */ /*-----( Declare Constants and Pin Numbers )-----*/ #define BUTTON1_PIN 8 #define BUTTON2_PIN 9 #define BUTTON3_PIN 10 #define BUTTON4_PIN 11 #define BUTTON5_PIN 12 #define LED_A_PIN 2 #define LED_B_PIN 3 #define LED_C_PIN 4 /*-----( Declare Variables )-----*/ int button1_State; int button2_State; int button3_State; int button4_State; int button5_State; int LED_A_State; int LED_B_State; int LED_C_State; void setup() /****** SETUP: RUNS ONCE ******/ { pinMode (BUTTON1_PIN, INPUT_PULLUP); pinMode (BUTTON2_PIN, INPUT_PULLUP); pinMode (BUTTON3_PIN, INPUT_PULLUP); pinMode (BUTTON4_PIN, INPUT_PULLUP); pinMode (BUTTON5_PIN, INPUT_PULLUP); LED_A_State = LOW; LED_B_State = LOW; LED_C_State = LOW; }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { /*--------( READ SENSORS )--------*/ button1_State = !digitalRead(BUTTON1_PIN); button2_State = !digitalRead(BUTTON2_PIN); button3_State = !digitalRead(BUTTON3_PIN); button4_State = !digitalRead(BUTTON4_PIN); button5_State = !digitalRead(BUTTON5_PIN); /*---------( MAKE DECISIONS )----------*/ if (button1_State) LED_A_State = HIGH; if (button2_State) LED_A_State = LOW; if (button3_State) { LED_B_State = ! LED_B_State; delay(250); } if (button4_State && button5_State) LED_C_State = HIGH; else LED_C_State = LOW; /*--------( TAKE ACTIONS )--------*/ digitalWrite(LED_A_PIN, LED_A_State); digitalWrite(LED_B_PIN, LED_B_State); digitalWrite(LED_C_PIN, LED_C_State); delay(50); //Wait for bounce to stop }//--(end main loop )--- //*********( THE END )***********
Example 2: Joystick controls pan-tilt servos, laser, Rotary Pot controls DC motor speed.
/* YourDuino Example: Servo Joystick Position, DC Motor Speed Control - Moves two Servomotors (usually on a pan-tilt kit) through a range of positions in response to the position of a joystick attached to 2 Analog inputs. Turns Laser ON-OFF - Controls speed of a DC motor with a rotary Potentiometer and Power FET - SEE the comments after "//" on each line below - CONNECTIONS: - Joystick connected to +5, Gnd, YourDuino Analog inputs A1 and A2 - Rotary Potentiometer connected +5, Gnd, Analog input A0 - Servo connectors plugged on YourDuinoRobo1 port 3 and 5 - If separate wires: - Servo Black to Gnd. - Servo Red or Orange (Center wire) to +5V - Servo White or Yellow to Signal (Pin 9 or 10) - V2.01 09/21/15 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ #include <Servo.h> // Comes with Arduino IDE /*-----( Declare Constants and Pin Numbers )-----*/ #define ServoHorizontalPIN 3 // Can be changed 3,5,6,9,10,11 #define ServoVerticalPIN 5 // Can be changed 3,5,6,9,10,11 #define DC_MotorPIN 6 // Controls DC Motor with NFET #define LaserPIN 13 // Control Laser on Pan-Tilt #define RotaryPotPin A0 // Analog input 0 (zero #define HorizontalPotPin A1 // Analog input 1 #define VerticalPotPin A2 // Analog input 2 #define JoyStickSwitchPin A3 // Used as Digital Input Pin #define ServoMIN_H 20 // Don't go to very end of servo travel #define ServoMAX_H 150 // which may not be all the way from 0 to 180. #define ServoMIN_V 0 // Don't go to very end of servo travel #define ServoMAX_V 55 // Restrict Laser Pointing /*-----( Declare objects )-----*/ Servo HorizontalServo; // create servo object to control a servo Servo VerticalServo; // create servo object to control a servo /*-----( Declare Variables )-----*/ int HorizontalPotValue; // User moves the Joystick Potentiometer int HorizontalServoPosition; // variable to store the servo position int VerticalPotValue; int VerticalServoPosition; int RotaryPotValue; // Changes as user rotates the knob int DC_Motor_PWM_Value; // Value mapped from RotaryPotValue int JoystickSwitchState; // Switch ON-OFF , Laser ON-OFF int LaserState; void setup() /****** SETUP: RUNS ONCE ******/ { HorizontalServo.attach(ServoHorizontalPIN); // attaches the servo to the servo object VerticalServo. attach(ServoVerticalPIN); // attaches the servo to the servo object pinMode(JoyStickSwitchPin, INPUT_PULLUP); // Set the Mode of pins pinMode(LaserPIN, OUTPUT); pinMode(DC_MotorPIN, OUTPUT); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { /*----------( READ SENSORS )----------*/ HorizontalPotValue = analogRead(HorizontalPotPin); // Get the value as user moves pot VerticalPotValue = analogRead(VerticalPotPin); // Get the value as user moves pot JoystickSwitchState = !digitalRead(JoyStickSwitchPin); // Invert the value RotaryPotValue = analogRead(RotaryPotPin); /*----------( MAKE DECISIONS )----------*/ // scale Potentiometer value to use it with the servo (value between MIN and MAX) HorizontalServoPosition = map(HorizontalPotValue, 0, 1023, ServoMIN_H , ServoMAX_H); VerticalServoPosition = map(VerticalPotValue, 0, 1023, ServoMIN_V , ServoMAX_V); DC_Motor_PWM_Value = map(RotaryPotValue, 0, 1023, 0 , 70); // 70 Max current/speed if (JoystickSwitchState) // Toggle the Laser ON-OFF if user pushes down on Jotstick. { LaserState = ! LaserState; // Invert the Laser ON-OFF State delay(250); } /*----------( TAKE ACTIONS )----------*/ HorizontalServo.write(HorizontalServoPosition); // Command servos to position. VerticalServo. write(VerticalServoPosition); delay(40); // wait for the servo to reach the position if (LaserState) digitalWrite(LaserPIN, HIGH); // Control Laser else digitalWrite(LaserPIN, LOW); analogWrite(DC_MotorPIN, DC_Motor_PWM_Value); // Set DC Motor Speed }//--(end main loop )--- //*********( THE END )***********
zz