l8test version
//setup
/*
In this sketch, a pushbutton is pressed triggering an LED to blink for a random interval of time (between 1 and 3 seconds),
followed by a set interval of time in which the LEDs are at full brightness (solid).
This sketch makes use of multi-line brackets (/*) for longer comments, in addition to standard comments (//).
We will use the function millis() to track the interval time of two LED events, blinking and solid.
Millis is a counter that returns the number of milliseconds since the Arduino board began running the current program.
See millis() in action on this Arduino Learning page: http://arduino.cc/en/Tutorial/BlinkWithoutDelay
Read more about millis on this Arduino Reference page: http://arduino.cc/en/Reference/Millis
We will also use a boolean variable. Boolean variables hold one of two values, true and false.
Read more about boolean on this Arduino Reference page: http://arduino.cc/en/Reference/BooleanVariables
We will refer to three intervals of time: the blinkinterval of an LED, the solidinterval of an LED, and the refreshTime of a servo motor.
*/
/* An integer, or int, is a datatype for number storage; the arduino software is programmed to recognize LOW and HIGH as binary values (0 and 1)
integer variable = value */
int ledPin = 4; // an LED is connected to pin 4
int ledPin2 = 5; // an LED is connected to pin 5
int ledPin3 = 6; // an LED is connected to pin 6
int ledPin4 = 7; // an LED is connected to pin 7
int ledPin5 = 8; // an LED is connected to pin 8
int ledPin6 = 9; // an LED is connected to pin 9
int ledPin7 = 10; // an LED is connected to pin 10
int ledPin8 = 11; // an LED is connected to pin 11
int ledPin9 = 12; // an LED is connected to pin 12
int ledPin10 = 13; // an LED is connected to pin 13
int inPin = 2; // a pushbutton is connected to pin 2
int Pinval = LOW; // a value to determine the on/off state of the pushbutton pin, LOW or HIGH
int ledPinState = LOW; // a value to determine the state of an LED pin, LOW or HIGH
/* Declare variables for two LED events, blinking and solid */
long blinkpreviousMillis = 0; // will store last time LED was updated
long blinkinterval = 50; // interval at which to blink (milliseconds)
long solidpreviousMillis = 0; // will store last time that an LED was solid versus blinking (using milliseconds)
long solidinterval = random(3000) + 1000; // a random interval of time determining how long the LED will blink before turning solid; at least 1 second (1000 mils) plus up to
//a random 3 seconds (3000 mils).
/* Declare servo variables. */
int servoPin = 3; // the control wire of a servo motor is connected to pin 3
int minPulse = 500; // minimum servo position
int maxPulse = 2500; // maximum servo position
/* Experiment with the pulse number. 650 should make it stop. A higher number will make the servo motor go one direction, lower the other direction. */
int pulse = 1200; // the frequency of the pulse wave sent to the servo motor in microseconds
long previousPulse = 0; // will store the length of time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
boolean buttonPress = false; // variable to determine whether button has been pressed; pressed equals true
boolean run = false;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(ledPin2, OUTPUT); // declare LED as output
pinMode(ledPin3, OUTPUT); // declare LED as output
pinMode(ledPin4, OUTPUT); // declare LED as output
pinMode(ledPin5, OUTPUT); // declare LED as output
pinMode(ledPin6, OUTPUT); // declare LED as output
pinMode(ledPin7, OUTPUT); // declare LED as output
pinMode(ledPin8, OUTPUT); // declare LED as output
pinMode(ledPin9, OUTPUT); // declare LED as output
pinMode(ledPin10, OUTPUT); // declare LED as output
pinMode(servoPin, OUTPUT); // declare servopin as output
pinMode(inPin, INPUT); // declare pushbutton as input
Serial.begin(9600); // opens a connection between microcontroller and computer to enable us to use the serial monitor
}
//loop
void loop(){
Pinval = digitalRead(inPin); // sets the Pinval to be equal to the value of the input of the pushbutton, LOW or HIGH
if (Pinval == LOW){ // if the pushbutton is pressed (returning a value of LOW)
buttonPress = true; // set the boolean variable "buttonPress" to true; "buttonPress" is a boolean variable; the value of this variable is "true"
}
if (buttonPress == true) { // if buttonPress equals true
/* use an "if else" control statement to determine which LED event, blinking or solid, to run next.*/
if (millis() - solidpreviousMillis < solidinterval) { // has the condition been met to turn the LED solid yet?
/* if the length of time ellapsed is less than the variable solidinterval, then go to the "blink LED" section of code,
else go to the "solid LED" section of code. */
/* blink LED
if the length of time ellapsed is greater than the variable blinkinterval, continue... */
if (millis() - blinkpreviousMillis > blinkinterval) { // has the condition been met to blink the LED yet?
blinkpreviousMillis = millis(); // remember the last time we blinked the LED
if (ledPinState == LOW){ // if the variable (ledPinState) is LOW, continue...
ledPinState = HIGH; // set the variable to HIGH
blinkinterval = random(100); // comment this line out if you do not want to use random time for your blinkinterval
}
else{ // if the variable (ledPinState) is HIGH, continue...
ledPinState = LOW; // set the variable to LOW
}
digitalWrite(ledPin, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin2, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin3, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin4, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin5, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin6, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin7, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin8, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin9, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
digitalWrite(ledPin10, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
}
//}
// servo
if (millis() - previousPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
previousPulse = millis(); // save the time of the last pulse
}
}
else{ // if the length of time ellapsed is greater than or equal to the variable solidinterval, go to the "solid LED" section of code.
/* solid LED - full brightness */
digitalWrite(ledPin,HIGH); // turn the LED on
digitalWrite(ledPin2,HIGH); // turn the LED on
digitalWrite(ledPin3,HIGH); // turn the LED on
digitalWrite(ledPin4,HIGH); // turn the LED on
digitalWrite(ledPin5,HIGH); // turn the LED on
digitalWrite(ledPin6,HIGH); // turn the LED on
digitalWrite(ledPin7,HIGH); // turn the LED on
digitalWrite(ledPin8,HIGH); // turn the LED on
digitalWrite(ledPin9,HIGH); // turn the LED on
digitalWrite(ledPin10,HIGH); // turn the LED on
/* determine how long the LEDs remain solid before the pushbutton cycle can be repeated */
if (millis() - solidpreviousMillis >= solidinterval + 3000) { // has the condition been met to turn off the LEDs and reset the "buttonPress" variable?
solidinterval = random(5000) + 2000; // set the interval of time before the LEDs go solid to random 5 seconds + 2 seconds
solidpreviousMillis = millis(); // remember the last time the LED was solid
digitalWrite(ledPin10,LOW); // turn the LED off
digitalWrite(ledPin9,LOW); // turn the LED off
digitalWrite(ledPin8,LOW); // turn the LED off
digitalWrite(ledPin7,LOW); // turn the LED off
digitalWrite(ledPin6,LOW); // turn the LED off
digitalWrite(ledPin5,LOW); // turn the LED off
digitalWrite(ledPin4,LOW); // turn the LED off
digitalWrite(ledPin3,LOW); // turn the LED off
digitalWrite(ledPin2,LOW); // turn the LED off
digitalWrite(ledPin,LOW); // turn the LED off
buttonPress = false; // the cycle is complete; the pushbutton can now be activated by the user again
}
}
}
}
second most recent code (with only 2 leds):
int servoPin = 2; // Control pin for servo motor (digital port)
int minPulse = 500; // Minimum servo position
int maxPulse = 2500; // Maximum servo position
int pulse = 0; // Amount to pulse the servo
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
int inButton = 3; // choose the input pin for a pushbutton (digital port)
int valButton = 0; // variable for reading the pin status
int dtime = 50;
int potpin = 1; // choose the pin for the potentiometer (analog port)
int valpot = 500;
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pinMode(inButton, INPUT);
pulse = minPulse; // Set the motor position value to the minimum
Serial.begin(9600);
}
void loop() {
valpot = analogRead(potpin) + 500; // read the value from the sensor
Serial.println(valpot);
valButton = digitalRead(inButton); // read input value
if (valButton == HIGH){
int dtime = 150;
// pulse = 2300; // changing this number will change the position it will start at
pulse = valpot;
}
else{
pulse = 500; // changing this number will change the other position it will go to
int dtime = 0;
}
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
delay(dtime);
}
}
void loop(){
Pinval = digitalRead(inPin); // sets the Pinval to be equal to the value of the input of the pushbutton, LOW or HIGH
if (Pinval == LOW){ // if the pushbutton is pressed (returning a value of LOW)
buttonPress = true; // set the boolean variable "buttonPress" to true; "buttonPress" is a boolean variable; the value of this variable is "true"
}
if (buttonPress == true) { // if buttonPress equals true
/* use an "if else" control statement to determine which LED event, blinking or solid, to run next.*/
if (millis() - solidpreviousMillis < solidinterval) { // has the condition been met to turn the LED solid yet?
/* if the length of time ellapsed is less than the variable solidinterval, then go to the "blink LED" section of code,
else go to the "solid LED" section of code. */
/* blink LED
if the length of time ellapsed is greater than the variable blinkinterval, continue... */
if (millis() - blinkpreviousMillis > blinkinterval) { // has the condition been met to blink the LED yet?
blinkpreviousMillis = millis(); // remember the last time we blinked the LED
if (ledPinState == LOW){ // if the variable (ledPinState) is LOW, continue...
ledPinState = HIGH; // set the variable to HIGH
blinkinterval = random(100); // comment this line out if you do not want to use random time for your blinkinterval
}
else{ // if the variable (ledPinState) is HIGH, continue...
ledPinState = LOW; // set the variable to LOW
}
digitalWrite(ledPin, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
}
//}
// servo
if (millis() - previousPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
previousPulse = millis(); // save the time of the last pulse
}
}
else{ // if the length of time ellapsed is greater than or equal to the variable solidinterval, go to the "solid LED" section of code.
/* solid LED - full brightness */
digitalWrite(ledPin,HIGH); // turn the LED on
digitalWrite(ledPin2,HIGH); // turn the LED on
/* determine how long the LEDs remain solid before the pushbutton cycle can be repeated */
if (millis() - solidpreviousMillis >= solidinterval + 3000) { // has the condition been met to turn off the LEDs and reset the "buttonPress" variable?
solidinterval = random(4000) + 1000; // set the interval of time before the LEDs go solid to random 3 seconds + 1 second
solidpreviousMillis = millis(); // remember the last time the LED was solid
digitalWrite(ledPin2,LOW); // turn the LED off
digitalWrite(ledPin,LOW); // turn the LED off
buttonPress = false; // the cycle is complete; the pushbutton can now be activated by the user again
}
}
}
}
/*version with a dc motor in place of servo*/
/* An integer, or int, is a datatype for number storage; the arduino software is programmed to recognize LOW and HIGH as binary values (0 and 1)
integer variable = value */
int ledPin = 3; // an LED is connected to pin 3
int ledPin2 = 4; // an LED is connected to pin 4
int inPin = 2; // a pushbutton is connected to pin 2
int Pinval = LOW; // a value to determine the on/off state of the pushbutton pin, LOW or HIGH
int ledPinState = LOW; // a value to determine the state of an LED pin, LOW or HIGH
/* Declare variables for two LED events, blinking and solid */
long blinkpreviousMillis = 0; // will store last time LED was updated
long blinkinterval = 50; // interval at which to blink (milliseconds)
long solidpreviousMillis = 0; // will store last time that an LED was solid versus blinking (using milliseconds)
long solidinterval = random(3000) + 1000; // a random interval of time determining how long the LED will blink before turning solid; at least 1 second (1000 mils) plus up to
//a random 3 seconds (3000 mils).
/* Declare variables. */
int dcPin = 9; // the control wire of a dc motor is connected to pin 9
int minPulse = 500; // minimum servo position
int maxPulse = 2500; // maximum servo position
/* Experiment with the pulse number. 650 should make it stop. A higher number will make the servo motor go one direction, lower the other direction. */
int pulse = 1200; // the frequency of the pulse wave sent to the servo motor in microseconds
long previousPulse = 0; // will store the length of time in milliseconds of the last pulse
int refreshTime = 1.5; // the time needed in between pulses
boolean buttonPress = false; // variable to determine whether button has been pressed; pressed equals true
int value = LOW; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(ledPin2, OUTPUT); // declare LED as output
pinMode(dcPin, OUTPUT); // declare dc motor as output
pinMode(inPin, INPUT); // declare pushbutton as input
Serial.begin(9600); // opens a connection between microcontroller and computer to enable us to use the serial monitor
}
void loop(){
Pinval = digitalRead(inPin); // sets the Pinval to be equal to the value of the input of the pushbutton, LOW or HIGH
if (Pinval == LOW){ // if the pushbutton is pressed (returning a value of LOW)
buttonPress = true; // set the boolean variable "buttonPress" to true; "buttonPress" is a boolean variable; the value of this variable is "true"
}
if (buttonPress == true){ // if buttonPress equals true
/* use an "if else" control statement to determine which LED event, blinking or solid, to run next.*/
if (millis() - solidpreviousMillis < solidinterval) { // has the condition been met to turn the LED solid yet?
/* if the length of time ellapsed is less than the variable solidinterval, then go to the "blink LED" section of code,
else go to the "solid LED" section of code. */
/* blink LED
if the length of time ellapsed is greater than the variable blinkinterval, continue... */
if (millis() - blinkpreviousMillis > blinkinterval) { // has the condition been met to blink the LED yet?
blinkpreviousMillis = millis(); // remember the last time we blinked the LED
if (ledPinState == LOW){ // if the variable (ledPinState) is LOW, continue...
// if (value == LOW){ // if the variable (ledPinState) is LOW, continue...
ledPinState = HIGH; // set the variable to HIGH
value = HIGH; // set the variable to HIGH
blinkinterval = random(100); // comment this line out if you do not want to use random time for your blinkinterval
digitalWrite(ledPin, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
// }
}
else{ // if the variable (ledPinState) is HIGH, continue...
ledPinState = LOW; // set the variable to LOW
digitalWrite(ledPin, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
}
}
digitalWrite(dcPin, HIGH); // turn the dc motor on or off depending on the value of the variable (ledPinState)
//servo
// if (millis() - previousPulse >= refreshTime) {
// digitalWrite(servoPin, HIGH); // Turn the motor on
// delayMicroseconds(pulse); // Length of the pulse sets the motor position
// digitalWrite(servoPin, LOW); // Turn the motor off
// previousPulse = millis(); // save the time of the last pulse
// }
}
else{ // if the length of time ellapsed is greater than or equal to the variable solidinterval, go to the "solid LED" section of code.
/* solid LED - full brightness */
digitalWrite(ledPin,HIGH); // turn the LED on
digitalWrite(ledPin2,HIGH); // turn the LED on
digitalWrite(dcPin, LOW); // turn the LED on or off depending on the value of the variable (ledPinState)
/* determine how long the LEDs remain solid before the pushbutton cycle can be repeated */
if (millis() - solidpreviousMillis >= solidinterval + 3000) { // has the condition been met to turn off the LEDs and reset the "buttonPress" variable?
solidinterval = random(3000) + 1000; // set the interval of time before the LEDs go solid to random 3 seconds + 1 second
solidpreviousMillis = millis(); // remember the last time the LED was solid
digitalWrite(ledPin2,LOW); // turn the LED off
digitalWrite(ledPin,LOW); // turn the LED off
digitalWrite(dcPin, LOW);
buttonPress = false; // the cycle is complete; the pushbutton can now be activated by the user again
}
}
}
}
/*version with the servo*/
/*
In this sketch, a pushbutton is pressed triggering an LED to blink for a random interval of time (between 1 and 3 seconds),
followed by a set interval of time in which the LEDs are at full brightness (solid).
This sketch makes use of multi-line brackets (/*) for longer comments, in addition to standard comments (//).
We will use the function millis() to track the interval time of two LED events, blinking and solid.
Millis is a counter that returns the number of milliseconds since the Arduino board began running the current program.
See millis() in action on this Arduino Learning page: http://arduino.cc/en/Tutorial/BlinkWithoutDelay
Read more about millis on this Arduino Reference page: http://arduino.cc/en/Reference/Millis
We will also use a boolean variable. Boolean variables hold one of two values, true and false.
Read more about boolean on this Arduino Reference page: http://arduino.cc/en/Reference/BooleanVariables
We will refer to three intervals of time: the blinkinterval of an LED, the solidinterval of an LED, and the refreshTime of a servo motor.
*/
/* An integer, or int, is a datatype for number storage; the arduino software is programmed to recognize LOW and HIGH as binary values (0 and 1)
integer variable = value */
int ledPin = 3; // an LED is connected to pin 3
int ledPin2 = 4; // an LED is connected to pin 4
int inPin = 2; // a pushbutton is connected to pin 2
int Pinval = LOW; // a value to determine the on/off state of the pushbutton pin, LOW or HIGH
int ledPinState = LOW; // a value to determine the state of an LED pin, LOW or HIGH
/* Declare variables for two LED events, blinking and solid */
long blinkpreviousMillis = 0; // will store last time LED was updated
long blinkinterval = 50; // interval at which to blink (milliseconds)
long solidpreviousMillis = 0; // will store last time that an LED was solid versus blinking (using milliseconds)
long solidinterval = random(3000) + 1000; // a random interval of time determining how long the LED will blink before turning solid; at least 1 second (1000 mils) plus up to
//a random 3 seconds (3000 mils).
/* Declare servo variables. */
int servoPin = 9; // the control wire of a servo motor is connected to pin 9
int minPulse = 500; // minimum servo position
int maxPulse = 2500; // maximum servo position
/* Experiment with the pulse number. 650 should make it stop. A higher number will make the servo motor go one direction, lower the other direction. */
int pulse = 1200; // the frequency of the pulse wave sent to the servo motor in microseconds
long previousPulse = 0; // will store the length of time in milliseconds of the last pulse
int refreshTime = 1.5; // the time needed in between pulses
boolean buttonPress = false; // variable to determine whether button has been pressed; pressed equals true
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(ledPin2, OUTPUT); // declare LED as output
pinMode(servoPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
Serial.begin(9600); // opens a connection between microcontroller and computer to enable us to use the serial monitor
}
void loop(){
Pinval = digitalRead(inPin); // sets the Pinval to be equal to the value of the input of the pushbutton, LOW or HIGH
if (Pinval == LOW){ // if the pushbutton is pressed (returning a value of LOW)
buttonPress = true; // set the boolean variable "buttonPress" to true; "buttonPress" is a boolean variable; the value of this variable is "true"
}
if (buttonPress == true){ // if buttonPress equals true
/* use an "if else" control statement to determine which LED event, blinking or solid, to run next.*/
if (millis() - solidpreviousMillis < solidinterval) { // has the condition been met to turn the LED solid yet?
/* if the length of time ellapsed is less than the variable solidinterval, then go to the "blink LED" section of code,
else go to the "solid LED" section of code. */
/* blink LED
if the length of time ellapsed is greater than the variable blinkinterval, continue... */
if (millis() - blinkpreviousMillis > blinkinterval) { // has the condition been met to blink the LED yet?
blinkpreviousMillis = millis(); // remember the last time we blinked the LED
if (ledPinState == LOW){ // if the variable (ledPinState) is LOW, continue...
ledPinState = HIGH; // set the variable to HIGH
blinkinterval = random(100); // comment this line out if you do not want to use random time for your blinkinterval
}
else{ // if the variable (ledPinState) is HIGH, continue...
ledPinState = LOW; // set the variable to LOW
}
digitalWrite(ledPin, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
}
//servo
if (millis() - previousPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
previousPulse = millis(); // save the time of the last pulse
}
}
else{ // if the length of time ellapsed is greater than or equal to the variable solidinterval, go to the "solid LED" section of code.
/* solid LED - full brightness */
digitalWrite(ledPin,HIGH); // turn the LED on
digitalWrite(ledPin2,HIGH); // turn the LED on
/* determine how long the LEDs remain solid before the pushbutton cycle can be repeated */
if (millis() - solidpreviousMillis >= solidinterval + 3000) { // has the condition been met to turn off the LEDs and reset the "buttonPress" variable?
solidinterval = random(3000) + 1000; // set the interval of time before the LEDs go solid to random 3 seconds + 1 second
solidpreviousMillis = millis(); // remember the last time the LED was solid
digitalWrite(ledPin2,LOW); // turn the LED off
digitalWrite(ledPin,LOW); // turn the LED off
buttonPress = false; // the cycle is complete; the pushbutton can now be activated by the user again
}
}
}
}
/*version before we added the servo*/
/*
In this sketch, a pushbutton is pressed triggering an LED to blink for a random interval of time (between 1 and 3 seconds),
followed by a set interval of time in which the LEDs are at full brightness (solid).
This sketch makes use of multi-line brackets (/*) for longer comments, in addition to standard comments (//).
We will use the function millis() to track the interval time of two LED events, blinking and solid.
Millis is a counter that returns the number of milliseconds since the Arduino board began running the current program.
See millis() in action on this Arduino Learning page: http://arduino.cc/en/Tutorial/BlinkWithoutDelay
Read more about millis on this Arduino Reference page: http://arduino.cc/en/Reference/Millis
We will also use a boolean variable. Boolean variables hold one of two values, true and false.
Read more about boolean on this Arduino Reference page: http://arduino.cc/en/Reference/BooleanVariables
We will refer to three intervals of time: the blinkinterval of an LED, the solidinterval of an LED, and the refreshTime of a servo motor.
*/
/* An integer, or int, is a datatype for number storage; the arduino software is programmed to recognize LOW and HIGH as binary values (0 and 1)
integer variable = value */
int ledPin = 3; // an LED is connected to pin 3
int ledPin2 = 4; // an LED is connected to pin 4
int inPin = 2; // a pushbutton is connected to pin 2
int Pinval = LOW; // a value to determine the on/off state of the pushbutton pin, LOW or HIGH
int ledPinState = LOW; // a value to determine the state of an LED pin, LOW or HIGH
/* Declare variables for two LED events, blinking and solid */
long blinkpreviousMillis = 0; // will store last time LED was updated
long blinkinterval = 50; // interval at which to blink (milliseconds)
long solidpreviousMillis = 0; // will store last time that an LED was solid versus blinking (using milliseconds)
long solidinterval = random(3000) + 1000; // a random interval of time determining how long the LED will blink before turning solid; at least 1 second (1000 mils) plus up to
//a random 3 seconds (3000 mils).
/* Declare servo variables. */
int servoPin = 9; // the control wire of a servo motor is connected to pin 9
int minPulse = 500; // minimum servo position
int maxPulse = 2500; // maximum servo position
/* Experiment with the pulse number. 650 should make it stop. A higher number will make the servo motor go one direction, lower the other direction. */
int pulse = 1200; // the frequency of the pulse wave sent to the servo motor in microseconds
long previousPulse = 0; // will store the length of time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
boolean buttonPress = false; // variable to determine whether button has been pressed; pressed equals true
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(ledPin2, OUTPUT); // declare LED as output
pinMode(servoPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
Serial.begin(9600); // opens a connection between microcontroller and computer to enable us to use the serial monitor
}
void loop(){
Pinval = digitalRead(inPin); // sets the Pinval to be equal to the value of the input of the pushbutton, LOW or HIGH
if (Pinval == LOW){ // if the pushbutton is pressed (returning a value of LOW)
buttonPress = true; // set the boolean variable "buttonPress" to true; "buttonPress" is a boolean variable; the value of this variable is "true"
}
if (buttonPress == true){ // if buttonPress equals true
/* use an "if else" control statement to determine which LED event, blinking or solid, to run next.*/
if (millis() - solidpreviousMillis < solidinterval) { // has the condition been met to turn the LED solid yet?
/* if the length of time ellapsed is less than the variable solidinterval, then go to the "blink LED" section of code,
else go to the "solid LED" section of code. */
/* blink LED
if the length of time ellapsed is greater than the variable blinkinterval, continue... */
if (millis() - blinkpreviousMillis > blinkinterval) { // has the condition been met to blink the LED yet?
blinkpreviousMillis = millis(); // remember the last time we blinked the LED
if (ledPinState == LOW){ // if the variable (ledPinState) is LOW, continue...
ledPinState = HIGH; // set the variable to HIGH
blinkinterval = random(100); // comment this line out if you do not want to use random time for your blinkinterval
}
else{ // if the variable (ledPinState) is HIGH, continue...
ledPinState = LOW; // set the variable to LOW
}
digitalWrite(ledPin, ledPinState); // turn the LED on or off depending on the value of the variable (ledPinState)
}
}
else{ // if the length of time ellapsed is greater than or equal to the variable solidinterval, go to the "solid LED" section of code.
/* solid LED - full brightness */
digitalWrite(ledPin,HIGH); // turn the LED on
digitalWrite(ledPin2,HIGH); // turn the LED on
/* determine how long the LEDs remain solid before the pushbutton cycle can be repeated */
if (millis() - solidpreviousMillis >= solidinterval + 3000) { // has the condition been met to turn off the LEDs and reset the "buttonPress" variable?
solidinterval = random(4000) + 1000; // set the interval of time before the LEDs go solid to random 3 seconds + 1 second
solidpreviousMillis = millis(); // remember the last time the LED was solid
digitalWrite(ledPin2,LOW); // turn the LED off
digitalWrite(ledPin,LOW); // turn the LED off
buttonPress = false; // the cycle is complete; the pushbutton can now be activated by the user again
}
}
}
}
earlier version with the series of carnival lights:
//starts here and is imperfect
int timer = 100; // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6; // the number of pins (i.e. the length of the array)
int servoPin = 9; // servo connected to digital pin 9
int myAngle; // angle of the servo (roughly in degrees) 0 - 180
int pulseWidth; // function variable
void setup()
{
int i;
// lights
for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
pinMode(pins[i], OUTPUT); // set each pin as an output
pinMode(2, OUTPUT); // sets pin D9 as output
}
//servo
void servoPulse(int servoPin, int myAngle) // servo function
{ // this is a function for determining our pulsewidth for the servo
pulseWidth = (myAngle * 6) + 320; // this determines our delay below (for a standard pot)
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // wait a very small amount (determined by pulsewidth)
digitalWrite(servoPin, LOW); // set servo low
delay(1); // refresh cycle of typical servos (20 ms)
}
void loop()
{
//lights
int i;
for (i = 0; i < num_pins; i++) { // loop through each pin...
digitalWrite(pins[i], HIGH); // turning it on,
delay(timer); // pausing,
digitalWrite(pins[i], LOW); // and turning it off.
}
for (i = num_pins - 1; i >= 0; i--) {
digitalWrite(pins[i], HIGH);
delay(timer);
digitalWrite(pins[i], LOW);
}
//servo
// cycle through every angle (rotate the servo 180 slowly)
for (myAngle=0; myAngle<=180; myAngle++) {
servoPulse(servoPin, myAngle);
delay(10);
servoPulse(servoPin, myAngle);
}
}
Comments (0)
You don't have permission to comment on this page.