13.5 Der Arduino gibt den Takt vor: Änderung der Wellenform

 

1 //Sine, Rect and Ramp output with stored array

2 //based on a sketch by Amanda Ghassaei

3 //http://www.instructables.com/id/Arduino-Audio- Output/

4 /*

5 * This program is free software; you can redistribute it and/or modify

6 * it under the terms of the GNU General Public License as published by

7 * the Free Software Foundation; either version 3 of the License, or

8 * (at your option) any later version.

9 *

10 */

11 byte sine[] = {127, 134, 142, 150, 158, 166, 173, 181, 188,195, 201, 2

12 213, 207, 201, 195, 188, 181, 173, 166, 158, 150, 142, 134, 127, 119, 1

13 40, 46, 52, 58, 65, 72, 80, 87, 95, 103, 111, 119};

14 byte rect [ ] =

15 {255,255,255,255,255,255,255,255,255,255,255,255,255,255,25 5,255,255,25

16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

17 byte triag[]= {23 ,27 ,31 ,35 ,39 ,43 ,47 ,51 ,55 ,59 ,63,67 ,71 ,75 ,

18 ,191 ,195 ,199 ,203 ,207 ,211 ,215 ,219 ,223 ,219 ,215 ,211,207 ,203 ,

19 ,91 ,87 ,83 ,79 ,75 ,71 ,67 ,63 ,59 ,55 ,51 ,47 ,43 ,39 ,35,31 ,27};

20 int del=30; // Delayfaktor

21 void setup(){

22 for (int i=0;i<8;i++){

23 pinMode(i,OUTPUT);

24 }

25 }

26 void loop(){

27 del=10+analogRead(0)/100;

28 for (int t=0;t<100;t++){//increment „t“

29 PORTD = sine[t];//Setze hier sine[t],rect[t] oder triag[t] ein

30 delayMicroseconds(del);

31 }

32 }

 

14.3 Mehrdimensionale Arrays für die Programmierung der Drum Machine

1 // Tabelle ausdrucken

2

3 for (int zeile=0; zeile<=3; zeile++){

4

5 for(int spalte=0; spalte >=4; spalte++) print(Tabelle[zeile][spalte]);

6

7 print“\n“); // Zeilenumbruch

8 }

 

Dies ist der komplette Sketch für unsere Drum Machine:

1 // Arduino- Drum Machine

2 // basierend auf dem Code von Sebastian Tomczak (April 2008)

3 // http://little-scale.blogspot.de/2008/04/arduino- drum-machine.html

4

5 int delayTime = 100;

6 int const barLength = 16;

7 int const barNr=6;

8

9 int LED1=8;

10 int LED2=9;

11 int LED3=10;

12 int LED4=11;

13

14 // Die Drum-sSequenz:

15

16 byte bars[barNr][barLength] = {

17 {1, 0, 1, 1, 1, 0, 1, 0, 1, 2, 2, 4, 1, 0, 1, 0},

18 {2, 0, 1, 1, 1, 0, 2, 2, 1, 2, 2, 4, 1, 0, 1, 0},

19 {1, 0, 1, 1, 1, 3, 2, 2, 1, 3, 1, 4, 1, 0, 1, 0},

20 {2, 0, 1, 1, 1, 3, 2, 3, 1, 2, 4, 3, 1, 0, 1, 0},

21 {1, 0, 1, 2, 1, 0, 4, 4, 2, 2, 3, 3, 1, 0, 1, 0},

22 {2, 0, 1, 1, 1, 2, 4, 4, 4, 3, 3, 3, 2, 2, 2, 0}

23 };

24

25

26

27 // Sampledaten

28

29 byte kick[] =

30 {127, 80, 42, 5, 165, 242, 241, 233, 128, 73, 48, 22, 127,69, 55, 113, 151, 183, 209, 217, 223, 228, 233, 215, 161,117, 91, 76, 65, 49, 37, 31, 31, 48, 83, 120, 146, 166,183, 198, 206, 210, 209, 199, 178, 145, 111, 88, 78, 73,69, 67, 72, 80, 88, 97, 109, 124, 137, 150, 163, 171, 174,172, 168, 160, 144, 125, 114, 110, 108, 104, 104, 106, 109,110, 112, 117, 124, 129, 135, 142, 145, 145, 143, 140, 137,132, 128, 125, 122, 119, 118, 119, 119, 119, 118, 118, 120,124, 126, 129, 132, 135, 137, 137, 135, 132, 131, 130, 129,128, 126, 126, 124, 123, 121, 120, 120, 122, 123, 124, 126,128, 129, 130, 130, 131, 131, 131, 130,130, 130, 129, 129,128, 126, 125, 125, 124, 124, 124, 124, 125, 126, 126, 128,128, 128, 129, 129, 129, 129, 129, 128, 128, 128, 128, 126,126, 126, 126, 126, 126, 126, 126, 126, 128, 127, 126, 128,128, 128, 128, 128, 128, 128, 128, 126, 126, 126, 126, 126,126, 126, 126, 128, 128, 128, 128, 128, 128, 128, 128, 128,128, 128, 128, 128, 126, 126, 126, 126, 126, 126, 126, 126,126, 126, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,128, 128, 128, 126, 126, 126, 126, 126, 126, 126, 126, 126,126, 126, 126, 126, 128, 128, 128, 128, 128, 128, 128, 128,128, 128, 128, 126, 126, 126, 126, 126, 126, 126, 126, 126,126};

31 byte snare[] =

32 {127, 215, 65, 212, 56, 102, 135, 122, 51, 201, 220, 46,175, 80, 152, 95, 123, 116, 184, 155, 59, 122, 100, 161, 143, 173, 101, 155, 97, 73, 112, 98, 176, 96, 140, 77, 134, 109, 132, 149, 112, 149, 97, 161, 98, 151, 98, 155, 149, 112, 157, 103, 133, 106, 167, 97, 166, 108, 129, 124, 136, 146, 124, 136, 129, 150, 94, 130, 105, 141, 146, 128, 129, 99, 150, 121, 141, 99, 142, 116, 131, 114, 118, 143, 127, 143, 115, 144, 120, 137, 109, 129, 131, 139, 129, 113, 144, 119, 145, 117, 135, 129, 134, 136, 124, 130, 130, 139, 121, 136, 121, 132, 128, 127, 126, 122, 130, 126, 138, 120, 136, 122, 131, 123, 130, 128, 127, 128, 118, 132, 125, 131, 122, 131, 125, 131, 122, 126, 128, 126, 129, 121, 129, 123, 132, 129, 127, 131, 123, 128, 125, 130, 123, 131, 123, 128, 131, 129, 128, 126, 125, 124, 131, 121, 124, 129, 130, 126, 124, 126, 127, 130, 125, 126, 128, 126, 128, 126, 126, 126, 126, 125, 128, 126, 126, 126, 126, 126, 126, 125, 128, 126, 126, 126, 126, 126, 126, 126, 126, 128, 128, 126, 128, 126, 127, 126, 128, 125, 127, 128, 128, 126, 126, 128, 126, 126, 128, 128, 128, 128, 128, 126, 128, 126, 126, 128, 128, 126, 126, 128, 128, 126, 126, 127, 126, 128, 126, 126, 128, 128, 128, 126, 126, 126, 128, 128, 126, 126, 126, 128, 128, 126, 128, 128, 126, 126};

33 byte hat[] =

34 {127, 128, 225, 217, 99, 38, 61, 153, 152, 144, 133, 73,122, 144, 65, 188, 87, 170, 164, 111, 122, 151, 114, 88,174, 77, 140, 92, 122, 141, 156, 124, 121, 123, 126, 133,132, 139, 119, 120, 127, 141, 130, 122, 129, 127, 132, 121,139, 118, 130, 131, 129, 132, 130, 134, 126, 128, 130, 126,122, 132, 129, 127, 131, 126, 128, 127, 126, 125, 127, 125,128, 125, 128, 128, 127, 127, 126, 127, 128, 128, 128, 127,127, 127, 127, 127, 128, 127, 127, 126, 127, 127, 128, 127,128, 126, 127, 128, 127, 127, 127, 127, 127, 127, 127, 127,127, 127, 127, 127, 127, 127, 127, 127, 127, 128, 127, 127,127, 127, 127, 127, 127, 127, 127, 127,127,127, 127, 127,127, 127, 127, 127, 127, 127, 127, 127, 127, 128, 128, 126,126, 128, 127, 126, 127, 126, 127, 127, 126, 127, 126, 127,127, 127, 127, 127, 126, 127, 127, 127, 126, 127, 127, 127,127, 127, 127, 127, 127, 127, 127, 126, 126, 126, 127, 127,127, 126, 127, 127, 127, 126, 127, 127, 126, 127, 127, 127,127, 127, 127, 126, 126, 126, 126, 126, 126, 126, 127, 127,126, 127, 126, 126, 127, 126, 127, 126, 126, 126, 126, 126,126, 126, 127, 127, 126, 127, 127, 127, 127, 126, 126, 127,127, 127, 126, 127, 126, 127, 127, 127, 127, 127, 126, 126,127, 127, 126, 127, 127, 127, 127, 126, 127, 127, 127, 127,127, 127, 127, 127};

35 byte crash[] =

36 {17, 128, 25, 217, 9, 138, 61, 133, 52, 144, 33, 73, 122,144, 65, 188, 187, 170, 14, 111, 222, 151, 114, 88, 174, 7,14, 92, 12, 141, 156, 12, 121, 1, 26, 13, 132, 139, 19,120, 17, 1, 130, 12, 129, 27, 13, 121, 139, 11, 30, 131,12, 132, 10, 134, 126, 28, 130, 12, 122, 132, 129, 127,131, 126, 128, 127, 126, 12, 27, 125, 128, 125, 18, 128,27, 127, 126, 127, 128, 128, 128, 127, 127, 127, 127, 127,12, 127, 127, 16, 127, 127, 12, 27, 128, 126, 127, 228,127, 127, 127, 227, 127, 127, 27, 17, 127, 127, 127, 127,127, 12, 127, 127, 27, 128, 127, 12, 127, 127, 17, 127,127, 127, 12, 127, 127, 127, 127, 127, 127, 27, 127, 127,127, 127, 127, 127, 127, 128, 128, 126, 126, 128, 127, 126,

37

38 void setup() {

39

40 // Pins 0 bis 11 auf Output

41

42 for (int k=0; k<12;k++) pinMode(k,OUTPUT);

43

44 }

45

46 void loop() {

47 for (int i=0; i<barNr;i++)

48 for(int j = 0; j < barLength; j ++)

49 playBeat(bars[i][j]);

50 }

51

52

53 // Playback Functions

54

55 void playBeat(byte beat) {

56 switch(beat){

57 case 0:

58 for(int i = 0; i < 256; i ++)

59 delayMicroseconds(analogRead(0) + 1);

60 break;

61

62

63 case 1:

64 digitalWrite(LED1,HIGH);

65 playKick();

66 digitalWrite(LED1,LOW);

67 break;

68

69

70 case 2:

71 digitalWrite(LED2,HIGH);

72 playSnare();

73 digitalWrite(LED2,LOW);

74 break;

75

76

77 case 3:

78 digitalWrite(LED3,HIGH);

79 playHat();

80 digitalWrite(LED3,LOW);

81 break;

82

83

84 case 4:

85 digitalWrite(LED4,HIGH);

86 playCrash();

87 digitalWrite(LED4,LOW);

88 break;

89 }

90 }

91

92 void playKick() {

93 for(int i = 0; i < 256; i ++) {

94 PORTD = kick[i];

95 delayMicroseconds(analogRead(0) + 1);

96 }

97 PORTD = 127;

98 }

99

100

101 void playSnare() {

102 for(int i = 0; i < 256; i ++) {

103 PORTD = snare[i];

104 delayMicroseconds(analogRead(0) + 1);

105 }

106 PORTD = 127;

107 }

108

109

110 void playHat() {

111 for(int i = 0; i < 256; i ++) {

112 PORTD = hat[i];

113 delayMicroseconds(analogRead(0) + 1);

114 }

115 PORTD = 127;

116 }

117

118 void playCrash() {

119 for(int i = 0; i < 256; i ++) {

120 PORTD = crash[i];

121 delayMicroseconds(analogRead(0) + 1);

123 }

123 PORTD = 127;

124 }

 

15.3.1 Hinderniserkennung

1 void FahrtVorwaerts (int a) // Setup für die Vorwärts-Fahrt voraus

2 {

3 digitalWrite (pinRB, LOW);

4 digitalWrite (pinRF, HIGH);

5 digitalWrite (pinLB, LOW);

6 digitalWrite (pinLF, HIGH);

7 delay (a * 100);

8 }

 

15.3.2 Entfernungsmessung

1 int UltraMessung() {

2 // Einen Impuls auslösen

3 digitalWrite(Trigger, LOW);

4 delayMicroseconds(2);

5 digitalWrite(Trigger, HIGH);

6 delayMicroseconds(5);

7 digitalWrite(Trigger, LOW);

8

9 // Messen, wann der reflektierte Impuls zurückkommt

10 int Laufzeit = pulseIn(Echo, HIGH);

11 // Pro Meter benötigt der Schall in Luft ca. 2,9 ms

12 // pulseIn misst Mikrosekunden, also 2,9 µs pro Millimeter

13 // Weg geht hin und zurück, also mal 2

14 int Entfernung = (double) Laufzeit / (2.9 * 2.0);

15 tln(“ mm“);

16 delay(100);

17 return Entfernung;

18

19 }

 

Dies ist der komplette Sketch für die Hinderniserkennung: (1)

1 // Ansteuerung fuer den autonomen Roboter:

2 // Das Fahrzeug erkennt Hindernisse im Fahrtweg und weicht ihnen aus

3

4 // Laden der Bibliothek für den Servo

5

6 #include &lt;Servo.h<

7

8 Servo myservo; // Set myservo

9

10 // Motoransteuerung

11 // Links

12 int pinLB = 6; // Define pin left after 6

13 int pinLF = 9; // Define the 9-pin front left

14

15 // Rechts

16 int pinRB = 10; // 10 pin definitions right rear

17 int pinRF = 11; // Define the 11-pin front right

18

 19 // Pin für die Ultraschallansteuerung

20

21 int Trigger = 2; // Trigger Pin

22 int Echo = 3; // Echo Pin

23 int Laufzeit; // Laufzeit des Schalls

24 int Entfernung; // Berechnete Entfernung

25

26

27 // Einstellung der Geschwindigkeit

28

29 int Fspeedd = 0; //Speed

30 int Rspeedd = 0; // Right speed

31 int Lspeedd = 0; // Left-speed

32

33 // Variablen für die Einstellung der Richtung

34

35 int Richtung = 0; // Front Left = 8 after = 2 = 4 Right= 6

36

37 int Fgo = 8; // Vorwaerts

38 int Rgo = 6; // Rechts

39 int Lgo = 4; // Links

40 int Bgo = 2; // Rückwärts

41

42

43 // Variablen fuer die Einstellung des Servos

44

45 int ServoGerade = 100; // Stellung fuer die gerade Messung

46 int ServoRechts = 177; // Stellung fuer die rechte Messung

 47 int ServoLinks = 0; // Stellung fuer die linke Messung

48

49 int ZeitServoStellung = 200; // settling time after steering servo motors

50

51

52 void setup ()

53 {

54 // Serielle Schnittstelle aktivieren

55 Serial.begin (9600);

56

57 // Motor

58 pinMode (pinLB, OUTPUT); // pin 8 (PWM)

59 pinMode (pinLF, OUTPUT); // pin 9 (PWM)

60 pinMode (pinRB, OUTPUT); // pin 10 (PWM)

61 pinMode (pinRF, OUTPUT); // pin 11 (PWM)

62

63 // Setup fuer den Ultraschall-Kopf

64 pinMode (Trigger, OUTPUT); // Der Trigger sendet das Signal

65 pinMode (Echo, INPUT); // Echo empfängt die Rueckkopplung

66

67 // Damit später durch HIGH-Setzen von TRIGGER ein Impuls

68 // ausgelöst werden kann, wird TRIGGER hier LOW gesetzt.

69 //digitalWrite(Trigger, LOW);

70

71 // TRIGGER muss mindestens 2µs LOW sein

72 //delayMicroseconds(2);

73

74

75 // Servo

76 myservo.attach (5); // Define servo motor output section 5 pin (PWM)

77

78 }

79

80 void FahrtVorwaerts (int a) // Setup für die Vorwärts-Fahrt

81 {

82 digitalWrite (pinRB, LOW); // The motor (rear right) action

83 digitalWrite (pinRF, HIGH);

84 digitalWrite (pinLB, LOW); // The motor (left rear) action

85 digitalWrite (pinLF, HIGH);

86 delay (a * 100);

87 }

88

89 void right (int b) // Turn right (single wheel)

90 {

91 digitalWrite (pinRB, LOW); // The motor (rear right) action

92 digitalWrite (pinRF, HIGH);

93 digitalWrite (pinLB, HIGH);

94 digitalWrite (pinLF, HIGH);

95 delay (b * 100);

96 }

97 void left (int c) // Turn left (single wheel)

98 {

99 digitalWrite (pinRB, HIGH);

100 digitalWrite (pinRF, HIGH);

101 digitalWrite (pinLB, LOW); // The motor (left rear) action

102 digitalWrite (pinLF, HIGH);

103 delay (c * 100);

104 }

105 void turnR (int d) // Turn right (wheel)

106 {

107 digitalWrite (pinRB, LOW); // The motor (rear right) action

108 digitalWrite (pinRF, HIGH);

109 digitalWrite (pinLB, HIGH);

110 digitalWrite (pinLF, LOW); // The motor (front left) action

111 delay (d * 100);

112 }

113 void turnL (int e) // Turn left (wheel)

114 {

115 digitalWrite (pinRB, HIGH);

116 digitalWrite (pinRF, LOW); // The motor (front right) action

117 digitalWrite (pinLB, LOW); // The motor (left rear) action

118 digitalWrite (pinLF, HIGH);

119 delay (e * 100);

120 }

121 void stopp (int f) // Stop

122 {

123 digitalWrite (pinRB, HIGH);

124 digitalWrite (pinRF, HIGH);

125 digitalWrite (pinLB, HIGH);

126 digitalWrite (pinLF, HIGH);

127 delay (f * 100);

128 }

129 void FahrtRueckwaerts (int g) // Check out

130 {

131

132 digitalWrite (pinRB, HIGH); // The motor (rear right) action

133 digitalWrite (pinRF, LOW);

134 digitalWrite (pinLB, HIGH); // The motor (left rear) action

135 digitalWrite (pinLF, LOW);

136 delay (g * 100);

137 }

138

139 void detection () // Entfernung vorne, links und rechts messen

140 {

141 MessungVorne (); // Read from front

142

143 if (Fspeedd &lt;100) // Ergibt die Messung weniger als 100 Millimeter

144 {

145 stopp (1); // Motorensteuerung stoppen

146 FahrtRueckwaerts (5); // Check out 0.2 seconds

147 }

148

149 if (Fspeedd &lt;250) // Ergibt die Messung weniger als 250 Millimeter

150 {

151 stopp (1); // Motorensteuerung stoppen

152 MessungLinks (); // Read from left

153 delay (ZeitServoStellung); // Wait for a stable servo motor

154 MessungRechts (); // Read from the right

155 delay (ZeitServoStellung); // Wait for a stable servo motor

156

157 if (Lspeedd > Rspeedd) // If the distance is greater than the right from the left

158 {

159 Richtung = Lgo; // Right away

160 }

161

162 if (Lspeedd <= Rspeedd) // If the left is less than or equal to the distance from the right

More..                                                                                                                                                             CONTINUE