Show Posts
Pages: 1 2 3 [4] 5
46  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 12:03:49 pm
Ahh right,

So in this line of code
 
Code:
square_out = square_in * 201UL / 1138UL;

where did the numbers 201 abd 1138 come from?
47  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 11:49:07 am
I have just found out some more info on the sqaurewave speed output from the ECU. The signal is 50% duty so the pulse length is determined by the frequency.The ECU updates the tachometer output every 50mS.

I'm hoping to read the signal in quicker than every 50ms to make sure that I'm not losing data.

I understand that the UNO is 16MHz, but how fast does that mean it could read and ouput data using my code?
48  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 09:28:06 am
Pylon, thanks for all your input.

The following formula moves your floats to integers (better uint32_t, precision: 3 digits behind the point):

Code:
square_out = square_in * 201UL / 1138UL ; // case ratio1
square_out = square_in * 420 / 1777; // case ratio2


Where would I add this code, in the main void loop?
so by adding that code, I could set all my variables to integers? What role does the "UL" have?

Is this the correct implimentation?

Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_pin = 2; //sqaure wave input from speed source
const int gear_pin = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const int ratio1 = 1/ 2.315;
const int ratio2 = 1/ 1.731;
const int FD = 1 / 4.7;
const int idler = 1.04;
const int pulses = 4; //pulses per simultated revolution
int square_in; //speed received
int square_out; //speed to send
int gear; // current gear selected
// the setup routine runs once when you press reset:
uint32_t lastEdge = 0;
byte state = 0;

void setup() {                
  // initialize the digital pin as an output.

  pinMode(speed_pin, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_pin, INPUT);

  attachInterrupt(0, registerPulse, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


  /*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
   */


  gear = digitalRead(gear_pin);
  if (gear == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


  else{        //ratio2 is selected
    square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


  /*
  //output the variable "square_out" as square wave to "Speed_out"
     */
    
     uint32_t outPulse = 500000UL / square_out; // get pulse length (not wave length)
  uint32_t m = micros();
  if (m - lastEdge >= outPulse) {
    state = ! state;
    digitalWrite(speed_out, state);
    lastEdge = m;
  }
    
    
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
49  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 07:50:00 am
An optocoupler may be your safest way to translate the voltage. Car voltages are not always clean 12V and can go as high as 18v. A voltage divider wouldn't protect against that. Since you will be driving an LED on the 12V side, just select a proper resistor to keep the current under the transmitter IF spec. Most I have looked at are around 50mA, so around 240 ohms (330 is probably a little safer which is about 36mA.)

Are you just using the arduino to translate the input pulses of 12v to output pulses of 5v or are you using the arduino to actually calculate the RPM? If the latter, I am not sure why you need to output the pulses on another pin.

the output from th ECU is regulated to 12v, however, I've just googled optocoupler and it looks that it could be a good idea, I assume these a fast enough?

I'm using the arduino to calculate the RPM and then output a different RPM based on another input (gear selector)
50  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 07:45:33 am
You're still missing the digitalRead
Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_pin = 2; //sqaure wave input from speed source
const int gear_pin = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send
int gear; // current gear selected
// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_pin, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_pin, INPUT);

  attachInterrupt(0, registerPulse, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


  /*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
   */


  gear = digitalRead(gear_pin);
  if (gear == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


  else{        //ratio2 is selected
    square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


  /*
  //output the variable "sqaure_out" as square wave to "Speed_out"
   */
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}


Sorry, I see what you mean now, I've now added a a digtialread for the gear. I don't understand how I can get the frequncy from pin2 into the registerpulse function??
51  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 07:05:26 am
I am still learning, only had an arduino about 3 weeks, and before that never touched C, I have done some VB, but that was also self taught so I don't want to pick up too many bad habbits early on. So far I've learnt the little I know from the exmaples and from this forum.

I've updated my code with an else function. So now that I've got the frequncy stored into the varilable "sqaure_in" how do I output a frequency?
Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);

attachInterrupt(0, registerPulse, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


else{        //ratio2 is selected
  square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
52  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 06:42:11 am
would an "else" statement be more effecient or is two "if" statements ok?
53  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 06:31:49 am
oops sorry missed that out too. You can tell I'm a newbee smiley-roll
Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);

attachInterrupt(0, registerPulse, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


if (gear_in == LOW) {        //ratio1 is selected
  square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
54  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 05:11:25 am
oops sorry smiley-roll forgot about that (again!)

Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


if (gear_in == LOW) {        //ratio1 is selected
  square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
55  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 07, 2013, 05:05:36 am
Thanks for your input,

I'd like to  update the output sqaurewave as quick as possible, accuracy wise, an error margin of around 5% would be acceptable.

I've merged your code into mine, have I understood your code correctly?

Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1

  square_out = square_in / 2 * ratio2 / idler * FD * pulses;



/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
56  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 06, 2013, 11:49:15 am
Square_ and square out is going to be a frequency, but thats the problem I have, I don't know how to input and output this?
57  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 06, 2013, 09:26:34 am
yes I was missing the digitalRead, sorry I'm very new to C

Thanks for the info on the code tags.

Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 23890;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (digitalRead(gear_in) == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


  if (digitalRead(gear_in) == LOW) {        //ratio2 is selected
  square_out = square_in / 2 * ratio2 / idler * FD * pulses;
 }//end of ratio2


/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}

58  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 06, 2013, 09:18:00 am
Thanks for your input, I now see my mistake with the if loop, I've now updated this.
To give you a bit more background information. I am trying to simualte axle speed on a gearbox, and my sqaurewave signal is an igntion pulse from a engine ECU. The output will be connected to a 3rd party data logger for testing.

I made an error when I estitmated the speed of the sqaure wave, it's actually a max of 100 pulses per second, but I would need the output speed to update quicker than every 50ms

Here is my updated code:
Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 23890;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


  if (gear_in == LOW) {        //ratio2 is selected
  square_out = square_in / 2 * ratio2 / idler * FD * pulses;
 }//end of ratio2


/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
59  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 06, 2013, 07:19:48 am
Sorry I was getting confused, the input and outputs are not PWM and are sqaure wave.

This is the code which I have come up with so far, the maths should be correct, and give you an idea of what I'm trying todo. What I'm unsure about is how to read in a sqaure wave and save the pulses as a variable, aslo read a variable and output as sqaure wave??

here is my code so far:
Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 23890;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1

  square_out = square_in / 2 * ratio2 / idler * FD * pulses;



/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
60  Using Arduino / Project Guidance / Re: Devide 12v square wave signal and output 5v squarewave on: February 05, 2013, 11:14:01 am
Thats great news,

So would I read the input as PWM?

Has anyone got an code where they input and output this kind of data?

Thanks

Jimster
Pages: 1 2 3 [4] 5