Go Down

Topic: Speed of a wheel measurement, light effect and sound effect (Read 946 times) previous topic - next topic

Yepino

Hello PaulS

 That's what I am missing here I don't know what to print. I mean I know what I want to print is the speed or the mph of the wheel, and then send that speed to the receiver Arduino well I was thinking to send only when the mph gets to 30mph and then make a sound and if the speed is less that 30mph then no sound.

 This is what I want, can you please guide me to the right direction thank you.

Yepino

For example look at this guys it says no matching function for hardware Serial (float), and this is on the receiver Arduino because mph is floating. These are  the thing that I am struggling.

Yepino

Hello guys

 I have tried it so many time but I can't figure it out, I really need your help especial on the data or speed in mph to be sent to the receiver Arduino. If you want to check my sketch I can send it again thank you for your cooperation.


Yepino

Hello

 After trying so many experiments on the project I tried something and I figured the error might be on the code of the receiver Arduino.

 Here is what I tried now, I thought what if I try to connect the hall sensor in the receiver Arduino only now I using 1 Arduino and try to interface the Sensor speed (or speed of the wheel in mph) to sound effect. Which means I got rid of the Lighting effect focus on the 2nd Arduino only.

 Here is my code and I will explain what the out come  of this code is:

Code: [Select]

#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <SPI.h>
#define hallPin  2
#define Speaker 9

int value;
TMRpcm tmrpcm;
char mychar;
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 maxhallCounter = 100;//min time (in ms) of one rotation (for debouncing)
int hallCounter;

void setup() {
  // put your setup code here, to run once:

   hallCounter = maxhallCounter;
   circumference = 2*3.14*radius;
   pinMode(ledpin,OUTPUT);
   pinMode(hallPin, INPUT);
   pinMode(Speaker, OUTPUT);
   

  // 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);
 
tmrpcm.speakerPin = 9;
if(!SD.begin(SD_ChipSelectPin)){
  Serial.println("SD fail");
  return;
}
tmrpcm.setVolume(5);
}

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 = (56.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);
  }

  Serial.println(mph);
  if(mph>15){
    digitalWrite(Speaker, HIGH);
    tmrpcm.play("23.wav");
    delay(3000);
  }
  else if(mph ==0 && mph <=15){
    digitalWrite(Speaker, LOW);
    delay(100);
  }
}




So what The out come was:

1. The speed doesn't look like to count as it was counting in the first one when there was master and receiver Arduino. It seems like it is only counting up to 15. something mph, and because of that you can see in my loop function in my if statement I changed the if (mph >30) to if (mph >15) any way.

2. The second problem is that the sensor is counting and the sound is triggered when the speed gets to 15mph but it doesn't stop playing even when the speed drops down to 0 mph. I guess this has something to do with the delays I have put in there.


 Now, my question is if I figure out to correct this, do you think that even with the master and receiver Arduino there is a chance for the project to work?

 Thank you again.


MorganS

Don't print or send any character.

Code: (Sender) [Select]

  if(ifTimeToPlayTheSound()) {
    digitalWrite(pin_number, HIGH);
  } else {
    digitalWrite(pin_number, LOW);
  }


Code: (Receiver) [Select]

  if(digitalRead(pin_number)) {
    playSound();
  }
"The problem is in the code you didn't post."

Yepino

Okay MorganS I will try it later thanks for your help, I hope this works.


Thank you very much

Yepino

Hello MorganS

 There is something I want to ask you about the code that you recommended me on Friday on #19, are you saying that I shouldn't use serial communication with the RX and TX. I mean is the digital read pin number high on the sender looks like the 2 Arduinos are connected via a digital pin.

 I will try it now and I will let you know the out come, but can you clarify what you meant with that please, thank you.

Yepino

Hey MorganS
 Is that Pin number, a digital output on he on the receiver Arduino? What is that actually I didn't get it.

Yepino

Hey Morgan

 Can you see what I did to the project and I would appreciate if you give some clue to your #19 code that you sent me Friday @MorganS thank you.

MorganS

Pick a pin to communicate between the Arduinos. Pin 7 or anything.

Record that number in a variable, to give it a name. Call it pinCommunication or anything you like.

It does not have to be the same pin on both, but it is easier to talk about it if it is.

Then the sender sets the pinMode to OUTPUT and the reciever makes it an INPUT.
"The problem is in the code you didn't post."

Yepino

Hello MorganS

 I really want to thank for your idea, my mistake was because I was thinking the hard way. Now they are communicating and the receiver Arduino is getting a signal and making a clicking noise which is a good thing.

 Now I will try to get rid of the delay on the sound code, because it is not making my wav sound file. So thank you again @MorganS.

Yepino

Hello Morgan

 The process of the project is working but I got a bit of a probelm , so what i did was I tried changing the code for the receiver Arduino because it was not plying as a result of Delay(). I got rid of the delay and I used millis(), but still nothing I mean there is a cracking like a signal of a second coming to the speaker but not the actual sound which I understand that the Arduinos are communicating.

 Here you go look at my code and see if there is any changes I should make thanks.

Sender Code

Code: [Select]


#include <FastLED.h>
#define hallPin  2//pin connected to read switch
#define LED_PIN 7
#define NUM_LEDS 256
#define Out_pin 8
//#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;
char value  = 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(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);
  // 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();
  }
  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();
  }
  if (mph > 30){
//    digitalWrite(Out_pin, HIGH); // I am not using this
    digitalWrite(Out_pin, HIGH);
  }
  else{
//    digitalWrite(Out_pin, LOW); // Not executing this
    digitalWrite(Out_pin, LOW);
  }
}



Receiver Code

Code: [Select]


#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <SPI.h>

#define Speaker 9
#define SPEEDIN 8


int SPEEDINVal = LOW;
TMRpcm tmrpcm;
char mychar;

long previousMillis = 0;
long interval = 4000;  // I did this 4 seconds because my sound is a 3 second file


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(4);
}


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 = HIGH){
       digitalWrite(Speaker, HIGH);
       tmrpcm.play("23.wav");
       previousMillis = millis();
   }
   
   else{
        digitalWrite(Speaker, LOW);
   } 
  }
}

Yepino

 Hey guys

 Any one with a help would be much appreciated thnak you a lot.

MorganS

Quote
Code: [Select]
  if(SPEEDINVal = HIGH){
Oops. Should have used ==

And maybe look at the StateChange example to see how to detect when it becomes high.
"The problem is in the code you didn't post."

Yepino

Hey Morgan

 Yeah I have used if (SPEEDINVal == HIGH), but it didn't work. I will see what I can do thanks.

Go Up