I have a heck of a project and I'm kind of new to this but picking it up quickly.
I am building a device that uses an fsr to light an LED, count the times the LED lights up with a 7 segment display and on top of that play a note for every time the LED lights up.
So far I have the FSR lighting the LED and counting. The problem that I'm having is with the sound. I can use tonepitchfollow but it's certainly doesn't sound very good for my project. The only thing I can find is how to light up an LED when a tone is played. I actually need it in reverse. I need the sound to play when the LED lights up. I can't seem to attach the sound to the FSR itself without tonepitchfollow and thats just a bunch of staticky noise.
I imagine this is something I can accomplish. The real work will begin later when I have to use 5 sensors subject to their own lights and musical notes. The counter will have to count all the LED lights.
I am using a Lilypad ATMega and not a breadboard.
I could use some direction!
This is my code so far:
#include <SoftwareSerial.h>
// These are the Arduino pins required to create a software seiral
// instance. We'll actually only use the TX pin.
const int softwareTx = 8;
const int softwareRx = 7;
const int buttonPin = 13;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
SoftwareSerial s7s(softwareRx, softwareTx);
unsigned int counter = 0; // This variable will count up to 65k
char tempString[10]; // Will be used with sprintf to create strings
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 13; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
int LEDbrightness;
void setup(){
Serial.begin(9600); // We'll send debugging information via the Serial monitor
pinMode(LEDpin, OUTPUT);
pinMode(buttonPin, INPUT);
// Must begin s7s software serial at the correct baud rate.
// The default of the s7s is 9600.
s7s.begin(9600);
clearDisplay();
}
void loop(){
// Magical sprintf creates a string for us to send to the s7s.
// The %4d option creates a 4-digit integer.
sprintf(tempString, "%4d", counter);
// This will output the tempString to the S7S
s7s.print(tempString);
setDecimals(0b00001000); // Sets digit 3 decimal on
int reading = digitalRead(buttonPin);
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
counter++;
}
}
}
lastButtonState = reading;
}
// Send the clear display command (0x76)
// This will clear the display and reset the cursor
void clearDisplay()
{
s7s.write(0x76); // Clear display command
}
// Turn on any, none, or all of the decimals.
// The six lowest bits in the decimals parameter sets a decimal
// (or colon, or apostrophe) on or off. A 1 indicates on, 0 off.
// [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1)
void setDecimals(byte decimals)
{
s7s.write(0x77);
s7s.write(decimals);
fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
// we'll need to change the range from the analog reading (0-1023) down to the range
// used by analogWrite (0-255) with map!
LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
// LED gets brighter the harder you press
analogWrite(LEDpin, LEDbrightness);
delay(100);
}