Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


Panneau latéral

projets:patatas-de-goma:dev:code

Codes Arduino

Standalone

#include <Tone.h>
 
// TIMER 0 : 5/6
// TIMER 1 : 9/10 OCR1A/OCR1B
// TIMER 2 : 3/11 (TONE)
 
Tone tone1;
int pinTone = 3;
int pinPwm = 9;
int periodePWM = 800; // max duty cycle
// 8000000/1000=8KHz
float pwm = 80; // %
 
void setup() {
  // 16Mhz(horloge)/2/1(prescaler) : 8Mhz
  TCCR1A = 0b10100000;
  TCCR1B = 0b00010001;
  ICR1 = periodePWM; // 8Mhz/periodPWM = frequence en Hz
 
  // PINS
  pinMode(pinPwm, OUTPUT);
  tone1.begin(pinTone);
}
 
void loop() {
  OCR1A = pwm * periodePWM * 0.01;
 
  tone1.play(NOTE_C4, 120);
  delay(500);
 
  tone1.play(NOTE_C5, 120);
  delay(500);
}

Avec Pure Data (SerialCommand)

// Controle avec Pure Data (SerialCommand)
 
// TIMER 0 : 5/6
// TIMER 1 : 9/10 OCR1A/OCR1B
// TIMER 2 : 3/11 (TONE)
 
#include <Tone.h>
#include <SerialCommand.h>
 
Tone tone1;
SerialCommand sCmd;
 
int pinTone1 = 3;
int pinPwm1 = 9;
 
int periodePWM = 800; //max duty cycle 800 marche avec polulu
 
void setup() {
  // 16Mhz(horloge)/2/1(prescaler) : 8Mhz
  TCCR1A = 0b10100000;
  TCCR1B = 0b00010001;
  ICR1 = periodePWM; // 8Mhz/PeriodePWM = Hz
 
  // PINS
  pinMode(pinPwm1, OUTPUT);
  tone1.begin(pinTone1);
 
  // SERIAL COMMAND
  Serial.begin(115200);
  sCmd.addCommand("kl1_tone", kl1_tone); // tone
  sCmd.addCommand("kl1_pwm", kl1_pwm); // intensite pwm %
  sCmd.addCommand("kl1_stop", kl1_stop);
  sCmd.setDefaultHandler(unrecognized);
  Serial.println("Ready");
}
 
void loop() {
  sCmd.readSerial();
}
 
// -------------- KLAXON 1 ----------------- //
void kl1_pwm() {
  float pwm;
  char *arg;
  arg = sCmd.next();
  if (arg != NULL) {
    pwm = atoi(arg);
    OCR1A = pwm * periodePWM * 0.01;
  }
}
 
void kl1_tone() {
  int aNumber, aNumber2;
  char *arg;
 
  arg = sCmd.next(); // 1st Argument
  if (arg != NULL) {
    aNumber = atoi(arg); 
  }
 
  arg = sCmd.next(); // 2nd Argument
  if (arg != NULL) {
    aNumber2 = atoi(arg); 
    tone1.play(aNumber, aNumber2);
  } else {
    tone1.play(aNumber);
  }
}
 
// STOP
void kl1_stop() {
  tone1.stop();
}
 
// ----------- UNRECOGNIZED ----------------- //
void unrecognized(const char *command) {
  Serial.println("Unrecognized...");
}

Standalone (avec LEDs)

// TIMER 0 : 5/6
// TIMER 1 : 9/10 OCR1A/OCR1B
// TIMER 2 : 3/11 (TONE)
 
int periodePWM = 3500; // max duty cycle = 20khz
 
int pinTone1 = 3;
int pinPwm1 = 9;
float pwm1 = 80; // %
 
void setup() {
  // 16Mhz(horloge) / 2 / 1(prescaler) : 8Mhz
  TCCR1A = 0b10100000;
  TCCR1B = 0b00010001;
  ICR1 = periodePWM; // 8Mhz/400=20KHz
  OCR1A = pwm1 * periodePWM * 0.01; // out of pwm
 
  // PINS
  pinMode(pinTone1, OUTPUT);
  pinMode(pinPwm1, OUTPUT);
}
 
void loop() {
 
  // LUMINOSITE DE LA LED
  analogWrite(pinTone1, 0);
  delay(1000);
 
  analogWrite(pinTone1, 50);
  delay(1000);
 
  analogWrite(pinTone1, 100);
  delay(1000);
 
  analogWrite(pinTone1, 255);
  delay(1000);
}
/home/resonancg/www/wiki/data/pages/projets/patatas-de-goma/dev/code.txt · Dernière modification: 2014/08/09 21:46 de resonance