Clapping music with Arduino: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 101: Line 101:
[[User:Laura Macchini/prototyping/ArduinoClappingMusic | Laura's Clapping Music with Arduino]]
[[User:Laura Macchini/prototyping/ArduinoClappingMusic | Laura's Clapping Music with Arduino]]


[[User:Laurier Rochon/prototyping/clapping music button | Laurier's Music with Arduino]]
[[User:Laurier Rochon/prototyping/clapping music button | Laurier's Clapping Music with Arduino]]

Revision as of 14:40, 26 October 2010

This exercise relies on the following topics in C:

  • variable types (int, char)
  • character arrays (strings)
  • loops
  • nested loops

Hello world.c

Arduino is C!

#include <stdio.h>

main ()
{
  printf("hello world\n");
}

Blink music

void setup()   {                
  pinMode(8, OUTPUT);
  for (int i=0; i<1000; i++) {
    digitalWrite(8, HIGH);
    delay(1);
    digitalWrite(8, LOW);
    delay(1);
  }
}

void loop () {}


change the argument to the delay...

for my refined control, try delayMicroseconds

void setup()   {                
  int x;
  // initialize the digital pin as an output:
  pinMode(8, OUTPUT);
  for (int i=0; i<1000; i++) {
    digitalWrite(8, HIGH);
    delayMicroseconds(250);
    digitalWrite(8, LOW);
    delayMicroseconds(250);
  }
}

tone

void setup() {
  tone(8, 500, 100);
  delay(100);
  tone(8, 600, 100);
  delay(100);
  tone(8, 700, 100);
  delay(100);
  tone(8, 800, 100);
  delay(100);
  tone(8, 900, 100);
  delay(100);
  
}

void loop() {}


Clapping music.c

Now, using a C string with the Clapping music pattern.

char pat[] = "xxx xx x xx ";

void setup() {
    int patlen = strlen(pat);
  
    for (int p=0; p<patlen; p++) {
        if (pat[p] == 'x') {
            tone(8, 500, 10);
        }
        delay(100);
    }
}

void loop () {}

Tone only technically supports playing one tone at a time; how might it be possible to still produce something like the 2 parts necessary for clapping music.

Laura's Clapping Music with Arduino

Laurier's Clapping Music with Arduino