diff options
| author | madias123 <matthias.lichtenegger@gmail.com> | 2015-03-23 23:42:57 +0100 |
|---|---|---|
| committer | madias123 <matthias.lichtenegger@gmail.com> | 2015-03-23 23:42:57 +0100 |
| commit | 488f6212a01e82a9430392b3bdd6af369d1d95cf (patch) | |
| tree | 1ab1599c89e4fb0298492872aacbd21e7d829178 /Arduino_STM32-MIDI/examples/Control | |
test
test
Diffstat (limited to 'Arduino_STM32-MIDI/examples/Control')
6 files changed, 358 insertions, 0 deletions
diff --git a/Arduino_STM32-MIDI/examples/Control/Arrays/Arrays.ino b/Arduino_STM32-MIDI/examples/Control/Arrays/Arrays.ino new file mode 100755 index 0000000..1ca4fbf --- /dev/null +++ b/Arduino_STM32-MIDI/examples/Control/Arrays/Arrays.ino @@ -0,0 +1,56 @@ +/* + Arrays + + Demonstrates the use of an array to hold pin numbers in order to + iterate over the pins in a sequence. Lights multiple LEDs in + sequence, then in reverse. + + Unlike the for loop tutorial, where the pins have to be + contiguous, here the pins can be in any random order. + + The circuit: + * LEDs from pins 2 through 7 to ground, through resistors + + created 2006 + by David A. Mellis + modified 5 Jul 2009 + by Tom Igoe + modifed for Maple + by LeafLabs + + http://leaflabs.com/docs/lang/cpp/array.html +*/ + +int delayTime = 100; // The higher the number, the slower the timing. +int ledPins[] = { + 2, 7, 4, 6, 5, 3 }; // An array of pin numbers to which LEDs are attached +int pinCount = 6; // The number of pins (i.e. the length of the array) + +void setup() { + int thisPin; + // The array elements are numbered from 0 to (pinCount - 1). + // Use a for loop to initialize each pin as an output: + for (int thisPin = 0; thisPin < pinCount; thisPin++) { + pinMode(ledPins[thisPin], OUTPUT); + } +} + +void loop() { + // Loop from the lowest pin to the highest: + for (int thisPin = 0; thisPin < pinCount; thisPin++) { + // Turn the pin on: + digitalWrite(ledPins[thisPin], HIGH); + delay(delayTime); + // Turn the pin off: + digitalWrite(ledPins[thisPin], LOW); + } + + // Loop from the highest pin to the lowest: + for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { + // Turn the pin on: + digitalWrite(ledPins[thisPin], HIGH); + delay(delayTime); + // Turn the pin off: + digitalWrite(ledPins[thisPin], LOW); + } +} diff --git a/Arduino_STM32-MIDI/examples/Control/ForLoopIteration/ForLoopIteration.ino b/Arduino_STM32-MIDI/examples/Control/ForLoopIteration/ForLoopIteration.ino new file mode 100755 index 0000000..f65706b --- /dev/null +++ b/Arduino_STM32-MIDI/examples/Control/ForLoopIteration/ForLoopIteration.ino @@ -0,0 +1,48 @@ +/* + for loop iteration + + Demonstrates the use of a for() loop. + Lights multiple LEDs in sequence, then in reverse. + + The circuit: + * LEDs from pins 2 through 7 to ground, through resistors + + created 2006 + by David A. Mellis + modified 5 Jul 2009 + by Tom Igoe + + http://leaflabs.com/docs/lang/cpp/for.html + + Modified for Maple + by LeafLabs +*/ + +int delayTime = 100; // The higher the number, the slower the timing. + +void setup() { + // Use a for loop to initialize each pin as an output: + for (int thisPin = 2; thisPin <= 7; thisPin++) { + pinMode(thisPin, OUTPUT); + } +} + +void loop() { + // Loop from the lowest pin to the highest: + for (int thisPin = 2; thisPin <= 7; thisPin++) { + // Turn the pin on: + digitalWrite(thisPin, HIGH); + delay(delayTime); + // Turn the pin off: + digitalWrite(thisPin, LOW); + } + + // Loop from the highest pin to the lowest: + for (int thisPin = 7; thisPin >= 2; thisPin--) { + // Turn the pin on: + digitalWrite(thisPin, HIGH); + delay(delayTime); + // Turn the pin off: + digitalWrite(thisPin, LOW); + } +} diff --git a/Arduino_STM32-MIDI/examples/Control/IfStatementConditional/IfStatementConditional.ino b/Arduino_STM32-MIDI/examples/Control/IfStatementConditional/IfStatementConditional.ino new file mode 100755 index 0000000..3afeeb1 --- /dev/null +++ b/Arduino_STM32-MIDI/examples/Control/IfStatementConditional/IfStatementConditional.ino @@ -0,0 +1,53 @@ +/* + Conditionals - If statement + + This example demonstrates the use of if() statements. It reads the + state of a potentiometer (an analog input) and turns on an LED only + if the LED goes above a certain threshold level. It prints the + analog value regardless of the level. + + The circuit: + * Potentiometer connected to pin 15. + Center pin of the potentiometer goes to the Maple pin. + Side pins of the potentiometer go to +3.3V and ground + + created 17 Jan 2009 + by Tom Igoe + + Ported to the Maple 27 May 2010 + by Bryan Newbold + + http://leaflabs.com/docs/lang/cpp/if.html +*/ + +// These constants won't change: + +const int analogPin = 15; // Pin that the sensor is attached to + +const int threshold = 400; // A random threshold level that's in + // the range of the analog input + +void setup() { + Serial.begin(115200); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor + // Initialize the built-in LED pin as an output: + pinMode(BOARD_LED_PIN, OUTPUT); + + // Initialize the potentiometer pin as an analog input: + pinMode(analogPin, INPUT_ANALOG); +} + +void loop() { + // Read the value of the potentiometer: + int analogValue = analogRead(analogPin); + + // If the analog value is high enough, turn on the LED: + if (analogValue > threshold) { + digitalWrite(BOARD_LED_PIN, HIGH); + } + else { + digitalWrite(BOARD_LED_PIN, LOW); + } + + // Print the analog value: + Serial.println(analogValue, DEC); +} diff --git a/Arduino_STM32-MIDI/examples/Control/WhileStatementConditional/WhileStatementConditional.ino b/Arduino_STM32-MIDI/examples/Control/WhileStatementConditional/WhileStatementConditional.ino new file mode 100755 index 0000000..b4f22a7 --- /dev/null +++ b/Arduino_STM32-MIDI/examples/Control/WhileStatementConditional/WhileStatementConditional.ino @@ -0,0 +1,82 @@ +/* + Conditionals - while statement + + This example demonstrates the use of while() statements. + + While the built-in button is pressed, the sketch runs the + calibration routine. The sensor readings during the while loop + define the minimum and maximum of expected values from the photo + resistor. + + This is a variation of the Analog > Calibration example. + + The circuit: + * Photo resistor connected from +3.3V to pin 15 + * 10K resistor connected from ground to pin 15 + * LED connected from digital pin 9 to ground through 220 ohm resistor + * 10K resistor attached from pin 2 to ground + + created 17 Jan 2009 + modified 25 Jun 2009 + by Tom Igoe + modified for Maple 13 February 2011 + by LeafLabs + + http://leaflabs.com/docs/lang/cpp/while.html +*/ + +// These constants won't change: +const int sensorPin = 2; // pin that the sensor is attached to +const int ledPin = 9; // pin that the LED is attached to + +// These variables will change: +int sensorMin = 4095; // minimum sensor value +int sensorMax = 0; // maximum sensor value +int sensorValue = 0; // the sensor value + +void setup() { + // set the LED pins as outputs and the switch pin as input: + pinMode(ledPin, OUTPUT); // LED on pin 9 + pinMode(BOARD_LED_PIN, OUTPUT); // Built-in LED + pinMode(BOARD_BUTTON_PIN, INPUT); // Built-in button +} + +void loop() { + // while the button is pressed, take calibration readings: + while (digitalRead(BOARD_BUTTON_PIN) == HIGH) { + // You could also use this: + //while (isButtonPressed()) { + calibrate(); + } + // signal the end of the calibration period + digitalWrite(BOARD_LED_PIN, LOW); + + // read the sensor: + sensorValue = analogRead(sensorPin); + + // apply the calibration to the sensor reading + sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 65535); + + // in case the sensor value is outside the range seen during calibration + sensorValue = constrain(sensorValue, 0, 65535); + + // fade the LED using the calibrated value: + pwmWrite(ledPin, sensorValue); +} + +void calibrate() { + // turn on the built-in LED to indicate that calibration is happening: + digitalWrite(BOARD_LED_PIN, HIGH); + // read the sensor: + sensorValue = analogRead(sensorPin); + + // record the maximum sensor value + if (sensorValue > sensorMax) { + sensorMax = sensorValue; + } + + // record the minimum sensor value + if (sensorValue < sensorMin) { + sensorMin = sensorValue; + } +} diff --git a/Arduino_STM32-MIDI/examples/Control/switchCase/switchCase.ino b/Arduino_STM32-MIDI/examples/Control/switchCase/switchCase.ino new file mode 100755 index 0000000..f1edb25 --- /dev/null +++ b/Arduino_STM32-MIDI/examples/Control/switchCase/switchCase.ino @@ -0,0 +1,55 @@ +/* + Switch statement + + Demonstrates the use of a switch statement. The switch statement + allows you to choose from among a set of discrete values of a + variable. It's like a series of if statements. + + To see this sketch in action, but the board and sensor in a well-lit + room, open the serial monitor, and and move your hand gradually down + over the sensor. + + The circuit: + * photoresistor from analog in 0 to +5V + * 10K resistor from analog in 0 to ground + + created 1 Jul 2009 + by Tom Igoe + + Ported to the Maple 27 May 2010 + by Bryan Newbold + + http://leaflabs.com/docs/lang/cpp/switchcase.html +*/ + +// These constants won't change: +const int sensorMin = 0; // sensor minimum, discovered through experiment +const int sensorMax = 600; // sensor maximum, discovered through experiment + +void setup() { + Serial.begin(115200); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor + pinMode(0, INPUT_ANALOG); +} + +void loop() { + // Read the sensor: + int sensorReading = analogRead(0); + // Map the sensor range to a range of four options: + int range = map(sensorReading, sensorMin, sensorMax, 0, 3); + + // Do something different depending on the range value: + switch (range) { + case 0: // your hand is on the sensor + Serial.println("dark"); + break; + case 1: // your hand is close to the sensor + Serial.println("dim"); + break; + case 2: // your hand is a few inches from the sensor + Serial.println("medium"); + break; + case 3: // your hand is nowhere near the sensor + Serial.println("bright"); + break; + } +} diff --git a/Arduino_STM32-MIDI/examples/Control/switchCase2/switchCase2.ino b/Arduino_STM32-MIDI/examples/Control/switchCase2/switchCase2.ino new file mode 100755 index 0000000..ace8ec8 --- /dev/null +++ b/Arduino_STM32-MIDI/examples/Control/switchCase2/switchCase2.ino @@ -0,0 +1,64 @@ +/* + Switch statement with serial input + + Demonstrates the use of a switch statement. The switch statement + allows you to choose from among a set of discrete values of a + variable. It's like a series of if statements. + + To see this sketch in action, open the Serial monitor and send any + character. The characters a, b, c, d, and e, will turn on LEDs. + Any other character will turn the LEDs off. + + The circuit: + * 5 LEDs attached to pins 2 through 6 through 220-ohm resistors + + created 1 Jul 2009 + by Tom Igoe + + Ported to the Maple 27 May 2010 + by Bryan Newbold + + http://leaflabs.com/docs/lang/cpp/switchcase.html +*/ + +void setup() { + Serial.begin(115200); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor + // Initialize the LED pins: + for (int thisPin = 2; thisPin <= 6; thisPin++) { + pinMode(thisPin, OUTPUT); + } +} + +void loop() { + // Read the sensor: + if (Serial.available() > 0) { + int inByte = Serial.read(); + // Do something different depending on the character received. + // The switch statement expects single number values for each + // case; in this example, though, you're using single quotes + // to tell the controller to get the ASCII value for the + // character. For example 'a' = 97, 'b' = 98, and so forth: + switch (inByte) { + case 'a': + digitalWrite(2, HIGH); + break; + case 'b': + digitalWrite(3, HIGH); + break; + case 'c': + digitalWrite(4, HIGH); + break; + case 'd': + digitalWrite(5, HIGH); + break; + case 'e': + digitalWrite(6, HIGH); + break; + default: + // Turn all the LEDs off: + for (int thisPin = 2; thisPin < 7; thisPin++) { + digitalWrite(thisPin, LOW); + } + } + } +} |
