Hello fellow professionals
I am here to ask a little question which I haven’t been able to resolve the problem in my project. First let me introduce you to my project, the aim of the project is to measure the speed of a wheel using a hall effect sensor and then according to the speed measured the RGB LED will change colours like less than 15mph red, above 30mph green and so on and after that when the speed reaches above 30mph there should be a sound of people cheering.
Anyways I got all working, the speed is measure, the lighting effect is doing its job and the sound is working. But, the sound only works in the beginning which means it only sound when the speed reached above 30mph in the first round after that when I stop the wheel and start it again it doesn’t sound at all other wise I have t start the Arduino UNO.
In my case I have used two Arduinos as in master and slave, the slave is holding the sound system and the master is working with the other 2. Here I will post my code and if you have anything in mind which might help me O would appreciate to share it with me.
I think the problem that I have to work is with the second Arduino which is doing the work for the sound system, to restart when the speed stops and starts again any way check my code and see if there is anything I have to add thank you.
Master Code
#include <FastLED.h>
#define hallPin 2//pin connected to read switch
#define LED_PIN 7
#define NUM_LEDS 256
#define Out_pin 8
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;
char value = 0;
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(Out_pin, OUTPUT);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(32);
// 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);
}
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();
}
else if (mph <= 15 && mph>0) {
for(int i=0; i<=255; i++){
leds[i] = CRGB(255,0,0);
}
FastLED.show();
}
else if (mph <= 30 && mph>15) {
for(int i=0; i<=255; i++){
leds[i] = CRGB(255,105,0);
}
FastLED.show();
}
else if (mph > 30) {
for(int i=0; i<=255; i++){
leds[i] = CRGB(0,255,0);
}
FastLED.show();
digitalWrite(Out_pin, HIGH);
}
else{
digitalWrite(Out_pin, LOW);
}
}
Slave Code
#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <SPI.h>
const int Speaker = 9;
const int SPEEDIN = 8;
int SPEEDINcounter = 0;
int SPEEDINVal = 0;
int LastSPEEDINVal = 0;
TMRpcm tmrpcm;
char mychar;
void setup() {
// put your setup code here, to run once:
pinMode(Speaker, OUTPUT);
pinMode(SPEEDIN, INPUT);
Serial.begin(9600);
tmrpcm.speakerPin = 9;
if(!SD.begin(SD_ChipSelectPin)){
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(5);
}
void loop() {
// put your main code here, to run repeatedly:
// unsigned long currentMillis = millis();
SPEEDINVal = digitalRead(SPEEDIN);
// if(currentMillis - previousMillis > interval){
// previousMillis = currentMillis;
if(SPEEDINVal != LastSPEEDINVal){
if(SPEEDINVal == HIGH){
SPEEDINcounter++;
digitalWrite(Speaker, HIGH);
tmrpcm.play("23.wav");
delay(3000);
// previousMillis = millis();
}
else{
digitalWrite(Speaker, LOW);
delay(1000);
}
}
LastSPEEDINVal = SPEEDINVal;
}