I have two breadboard projects set up. Separately, they work perfectly. It's just combining them to work with just the one analog input from pin 5 that is causing the problem.
First code:
// EMF Detector with LEDs
#define NUMREADINGS 15 // raise this number to increase data smoothing
int senseLimit = 15; // raise this number to decrease sensitivity (up to 1023 max)
int probePin = 5; // analog 5
int val = 0; // reading from probePin
// connections to LEDs with resistors in series
int LED6 = 6;
int LED5 = 5;
int LED4 = 4;
int LED3 = 3;
int LED2 = 2;
// variables for smoothing
int readings[NUMREADINGS]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // final average of the probe reading
void setup() {
pinMode(2, OUTPUT); // specify LED outputs
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(9600); // initiate serial connection for debugging/etc
for (int i = 0; i < NUMREADINGS; i++)
readings[i] = 0; // initialize all the readings to 0
}
void loop() {
val = analogRead(probePin); // take a reading from the probe
if(val >= 1){ // if the reading isn't zero, proceed
val = constrain(val, 1, senseLimit); // turn any reading higher than the senseLimit value into the senseLimit value
val = map(val, 1, senseLimit, 1, 1023); // remap the constrained value within a 1 to 1023 range
total -= readings[index]; // subtract the last reading
readings[index] = val; // read from the sensor
total += readings[index]; // add the reading to the total
index = (index + 1); // advance to the next index
if (index >= NUMREADINGS) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning
average = total / NUMREADINGS; // calculate the average
if (average > 50){ // if the average is over 50 ...
digitalWrite(LED2, HIGH); // light the first LED
}
else{ // and if it's not ...
digitalWrite(LED2, LOW); // turn that LED off
}
if (average > 150){ // and so on ...
digitalWrite(LED3, HIGH);
}
else{
digitalWrite(LED3, LOW);
}
if (average > 250){
digitalWrite(LED4, HIGH);
}
else{
digitalWrite(LED4, LOW);
}
if (average > 350){
digitalWrite(LED5, HIGH);
}
else{
digitalWrite(LED5, LOW);
}
if (average > 450){
digitalWrite(LED6, HIGH);
}
else{
digitalWrite(LED6, LOW);
}
Serial.println(val); // use output to aid in calibrating
}
}
Second code (the sound from the speaker works just fine here):
// Pitch follower
//Plays a pitch that changes based on a changing analog input
//circuit:
//* 8-ohm speaker on digital pin 8
// * photoresistor on analog 0 to 5V
// * 4.7K resistor on analog 0 to ground
//This example code is in the public domain.
//http://arduino.cc/en/Tutorial/Tone2
void setup() {
// initialize serial communications (for debugging only):
Serial.begin(9600);
}
void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the analog input range (in this case, 400 - 1000 from the photoresistor)
// to the output pitch range (120 - 1500Hz)
// change the minimum and maximum input numbers below
// depending on the range your sensor's giving:
int thisPitch = map(sensorReading, 400, 1000, 120, 1500);
// play the pitch:
tone(9, thisPitch, 10);
delay(1); // delay in between reads for stability
}