Hello my friends
I am going to try to explain my project first and then put my code and see if I can get some help.
My project is to measure the speed of a bike and the lighting effect happens to different speeds of the wheel. For example if the speed is below 15mph the RGB LED will lit red and when the speed gets to over 30mph the LED will lit Green and so on, and at the same time when the speed gets to 30mph I wanted a sound to be played.
Now in my case I was able to do the speed measurement and the lighting effect perfectly, now the problem is when I add the code for the SD card or sound, it doesn't work properly. When I combined the codes even the light only lit green which means it ain't working properly. so what I did was I tried to so make the 2nd part or the sound effect on another Arduino board and Voila the speed measurement and the lighting effect works except there is no sound playing at 30mph.
So what I think I am doing a mistake is on the I2C communication between the 2 Arduinos, I connected them like in A4 and A5 of the 2 Arduinos and common ground for both of them as well, but the code doesn't seem write. Can some body help me by directing me into a right direction please, Thanks a lot.
Code for the Master Arduino or the Speed and Lighting effect
//calculations
//tire radius ~ 10 inches
//circumference = pi*2*r =~62.8 inches
//max speed of 35mph =~ 616inches/second
//max rps =~7.25
#include <FastLED.h>
#define hallPin 2//pin connected to read switch
#define LED_PIN 7
#define NUM_LEDS 256
#include <Wire.h>
CRGB leds[NUM_LEDS];
//storage variables
float radius = 10;// tire radius (in inches)- CHANGE THIS FOR YOUR OWN BIKE
int ledpin = 3;
int hallVal;
long timer = 0;// time between one full rotation (in ms)
float mph = 0.00;
float circumference;
int x = 0;
//boolean backlight;
int maxhallCounter = 100;//min time (in ms) of one rotation (for debouncing)
int hallCounter;
void setup(){
hallCounter = maxhallCounter;
circumference = 2*3.14*radius;
pinMode(LED_PIN,OUTPUT);
pinMode(ledpin,OUTPUT);
pinMode(hallPin, INPUT);
// pinMode(Speaker, OUTPUT);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(32);
// checkBacklight();
// Serial.write(12);//clear
// TIMER SETUP- the timer interrupt allows preceise timed measurements of the reed switch
//for mor info about configuration of arduino timers see http://arduino.cc/playground/Code/Timer1
cli();//stop interrupts
//set timer1 interrupt at 1kHz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;
// set timer count for 1khz increments
OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*8) - 1
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//END TIMER SETUP
Serial.begin(9600);
// Start the I2C Bus as Master
Wire.begin();
}
ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch
hallVal = digitalRead(hallPin);//get val of hall sensor
if (hallVal==LOW){//if reed switch is closed
if (hallCounter == 0){//min time between pulses has passed
mph = (53.8*float(circumference))/float(timer);//calculate miles per hour
timer = 0;//reset timer
hallCounter = maxhallCounter;//reset hallCounter
}
else{
if (hallCounter > 0){//don't let reedCounter go negative
hallCounter -= 1;//decrement reedCounter
}
}
}
else{//if reed switch is open
if (hallCounter > 0){//don't let reedCounter go negative
hallCounter -= 1;//decrement reedCounter
}
}
if (timer > 2000){
mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
}
else{
timer += 1;//increment timer
}
}
void loop(){
if(hallVal == LOW){
digitalWrite(ledpin, HIGH);
}
else{
digitalWrite(ledpin, LOW);
}
//print mph once a second
Serial.println(mph);
if (mph == 0) {
for(int i=0; i<=255; i++){
leds[i] = CRGB(0,0,0);
}
FastLED.show();
}
if (mph <= 15 && mph>0) {
for(int i=0; i<=255; i++){
leds[i] = CRGB(255,0,0);
}
FastLED.show();
}
if (mph <= 30 && mph>15) {
for(int i=0; i<=255; i++){
leds[i] = CRGB(255,105,0);
}
FastLED.show();
}
if (mph > 30) {
for(int i=0; i<=255; i++){
leds[i] = CRGB(0,255,0);
}
FastLED.show();
}
int x = digitalRead(mph);
Wire.beginTransmission(9); // transmit to device #9
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
x++; // Increment x
if (x > 30) x = 0; // `reset x once it gets 30
// delay(500);
}
And here is the code for the slave Arduino or for the sound
#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <SPI.h>
#define Speaker 9
#include <Wire.h>
int x = 0;
TMRpcm tmrpcm;
char mychar;
void setup() {
// put your setup code here, to run once:
pinMode(Speaker, OUTPUT);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
tmrpcm.speakerPin = 9;
if(!SD.begin(SD_ChipSelectPin)){
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(5);
}
void receiveEvent(int howMany){
while (1 < Wire.available()){
char c = Wire.read();
Serial.print(c);
}
int x = Wire.read(); //read one character from I2C
Serial.println(x);
}
void loop() {
// put your main code here, to run repeatedly:
if (x == 30){
digitalWrite(Speaker, HIGH);
tmrpcm.play("23.wav");
delay(3000);
}
else{
digitalWrite(Speaker, LOW);
delay(100);
}
}