Permalink
Browse files

Adds to sound speaker with potentiometer.

  • Loading branch information...
two-pack committed Jul 22, 2014
1 parent 437ccf8 commit 95f1d9b37b9da8f1c2cdd8373a7a8992327e8178
Showing with 116 additions and 0 deletions.
  1. BIN speaker2/circut.fzz
  2. +28 −0 speaker2/speaker2.ino
  3. +32 −0 speaker2/tone.ino
  4. +56 −0 speaker2/toneTest.ino
View
Binary file not shown.
View
@@ -0,0 +1,28 @@
extern const int SPEAKER;
//#define TEST
#ifdef TEST
#include <ArduinoUnit.h>
void setup() {
Serial.begin(9600);
}
void loop() {
Test::run();
}
#else
void setup() {
Serial.begin(9600);
pinMode(SPEAKER, OUTPUT);
}
void loop() {
sound();
delay(10);
}
#endif
View
@@ -0,0 +1,32 @@
const int SPEAKER = 11; // pin number of speaker.
const int SWITCH = 0; // Analog In 0.
void sound() {
int value = analogReadW(SWITCH);
if(value < 0) {
toneW(SPEAKER, 0);
} else if(value > 1023) {
toneW(SPEAKER, 1023);
} else if(value == 0) {
noToneW(SPEAKER);
} else {
toneW(SPEAKER, value);
}
}
#ifndef TEST
void toneW(int pin, int frequency) {
tone(pin, frequency);
}
void noToneW(int pin) {
noTone(pin);
}
int analogReadW(int pin) {
return analogRead(pin);
}
#endif
View
@@ -0,0 +1,56 @@
#ifdef TEST
static int mockAnalogInputs[] = {0, 0, 0, 0, 0, 0}; // value of Analog input.
static int mockPins[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // value of Pins.
static int flagNoToneCalled[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
test(no_tone_when_switch_is_0) {
flagNoToneCalled[SPEAKER] = 0;
mockAnalogInputs[SWITCH] = 0;
sound();
assertEqual(1, flagNoToneCalled[SPEAKER]);
}
test(1023_tone_when_switch_is_1023) {
mockPins[SPEAKER] = 100;
mockAnalogInputs[SWITCH] = 1023;
sound();
assertEqual(1023, mockPins[SPEAKER]);
}
test(1023_tone_when_switch_is_1024) {
mockPins[SPEAKER] = 100;
mockAnalogInputs[SWITCH] = 1024;
sound();
assertEqual(1023, mockPins[SPEAKER]);
}
test(1023_tone_when_switch_is_less_than_0) {
mockPins[SPEAKER] = 100;
mockAnalogInputs[SWITCH] = -1;
sound();
assertEqual(0, mockPins[SPEAKER]);
}
void toneW(int pin, int frequency) {
mockPins[pin] = frequency;
}
void noToneW(int pin) {
flagNoToneCalled[pin] = 1;
}
int analogReadW(int pin) {
return mockAnalogInputs[pin];
}
#endif

0 comments on commit 95f1d9b

Please sign in to comment.