wrote a value to a pin and trying to read it using pulsein(). Trouble is when I write 48, I get anywhere from 46 to 49 back when I use the map function to convert is back to the ascii value. How exactly do you make sense of pulseIn????
pulseIn() is a function that measures the length of a pulse, it cannot read numbers like 48.
- please post your code
- please tell the goal of your project
(assuming 48 is a pulse length)
The implementation of pulseIn() is affected by e.g. the number of interrupts.
So pulseIn() is an approximation at best - most of the time it is within 1 or 2 uSec and that is what your seeing
Ok, here is my code.
/*
PulseIn sketch
displays duration of high and low pulses from analogWrite
*/
const int inputPin = 3; // analog output pin to monitor
unsigned long inputVal; // value inputed by user on serial
unsigned long val; // this will hold the value from pulseIn
void setup()
{
Serial.begin(9600);
analogWrite(inputPin, 128);
Serial.print("Writing 128 to pin ");
Serial.print(inputPin);
printPulseWidth(inputPin);
analogWrite(inputPin, 254);
Serial.print("Writing 254 to pin ");
Serial.print(inputPin);
printPulseWidth(inputPin);
Serial.println("Enter a value");
}
void loop()
{
if (Serial.available() > 0) { // if the data came
inputVal = Serial.read(); // read byte
Serial.print("Writing ");
Serial.print(inputVal);
Serial.print(" to pin ");
Serial.print(inputPin);
analogWrite(inputPin, inputVal);
printPulseWidth(inputPin);
Serial.println("Enter a value");
}
}
void printPulseWidth(int pin)
{
val = pulseIn(pin, HIGH);
Serial.print(": High Pulse width = ");
Serial.print(map(val, 0, 2048, 0, 255));
val = pulseIn(pin, LOW);
Serial.print(", Low Pulse width = ");
Serial.println(map(val, 1248, 3408, 0, 255));
}
I have also been experimenting with this next code I found, which may get around the problem but I cant really make heads or tails of it yet.
const int inputCapturePin = 8; // input pin fixed to internal Timer
const int ledPin = 13;
const int prescale = 8; // prescale factor (each tick 0.5 us @16MHz)
const byte prescaleBits = B010; // see Table 18-1 or data sheet
// calculate time per counter tick in ns
const long precision = (1000000/(F_CPU/1000)) * prescale ;
const int numberOfEntries = 64; // the max number of pulses to measure
const int gateSamplePeriod = 1000; // the sample period in milliseconds
const int inputPin = 8; // analog output pin to monitor
unsigned long inputVal; // value inputed by user on serial
unsigned long val; // this will hold the value from pulseIn
volatile byte index = 0; // index to the stored readings
volatile byte gate = 0; // 0 disables capture, 1 enables
volatile unsigned int results[numberOfEntries]; // note this is 16 bit value
/* ICR interrupt vector */
ISR(TIMER1_CAPT_vect)
{
TCNT1 = 0; // reset the counter
if(gate)
{
if( index != 0 || bitRead(TCCR1B ,ICES1) == true) // wait for rising edge
{ // falling edge was detected
if(index < numberOfEntries)
{
results[index] = ICR1; // save the input capture value
index++;
}
}
}
TCCR1B ^= _BV(ICES1); // toggle bit to trigger on the other edge
}
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(inputCapturePin, INPUT); // ICP pin (digital pin 8 on Arduino) as input
TCCR1A = 0 ; // Normal counting mode
TCCR1B = prescaleBits ; // set prescale bits
TCCR1B |= _BV(ICES1); // enable input capture
bitSet(TIMSK1,ICIE1); // enable input capture interrupt for timer 1
Serial.println("pulses are sampled while LED is lit");
Serial.print( precision); // report duration of each tick in microseconds
Serial.println(" microseconds per tick");
}
// this loop prints the number of pulses in the last second, showing min
// and max pulse widths
void loop()
{
if (Serial.available() > 0) { // if the data came
inputVal = Serial.read(); // read byte
Serial.print("Writing ");
Serial.print(inputVal);
Serial.print(" to pin ");
Serial.print(inputPin);
analogWrite(inputPin, inputVal);
// printPulseWidth(inputPin);
Serial.println("Enter a value 0 - 255");
}
digitalWrite(ledPin, LOW);
delay(gateSamplePeriod);
digitalWrite(ledPin, HIGH);
gate = 1; // enable sampling
delay(gateSamplePeriod);
gate = 0; // disable sampling
if(index > 0)
{
Serial.println("Durations in Microseconds are:") ;
for( byte i=0; i < numberOfEntries; i++)
{
long duration;
duration = results[i] * precision; // pulse duration in nanoseconds
if(duration >0)
Serial.println(map((duration / 1000), 0, 9999, 0, 255)); // duration in microseconds
}
index = 0;
}
}
That last code was found here: Inkling
I am attempting to hack my house alarm, the post containing the info can be found here. Need help hacking inner range concept 2000 house alarm system - Device Hacking - Arduino Forum
Wiring:
Control Pannel__
| | | | <--Data Wire
Keypad---Arduino
4 wires. Pos, Neg, Timer, Data
attempting to hack my house alarm
I hope you've got sympathetic insurers.
lol, yeah. We bought the house 2 years ago but the previous owner couldn't remember the codes to the alarm. The story is that he upgraded it to the 4000 or something but took that with him to his new home. He left us his old 2000 model but couldn't remember the code. Im now trying to find out.
const int inputPin = 3; // analog output pin to monitor
Why is an output called inputPin?
I gave up trying to make sense of your code at that point.
Because its fashioned off someone else's code.
Stuperfied:
Because its fashioned off someone else's code.
Who's code is it now?
Who's Arduino is it running on?
Who's responsibility is it to use meaningful names?
Im sorry, may I have some help now please?
Help with what?
What does the code do that you don't want or expect it to do?
What doesn't the code do that you want or expect it to?
Ok, np. I was trying to test the first code with known values before using it. I typed "0" in the console, the code interprets that as "48" which is the ascii equivalent, this was unexpected but ok. Next the code writes "48" to pin 3, then reads pin 3 with pulseIn(). All ok untill I map what its read back to "255" (ascii) and output to the console. The results should be "48", however I get back anywhere from "46" - "49". Im of the understanding that this is because of interrupts, that's fine but it doesn't help me.
Tried the second code but got back values from my alarm which were around 9000, no idea what that means because I don't understand much of that code.
Im trying to use this code to intercept data being sent between my house alarm keypad and the control pannel. The idea is to do a bruit force but first I have to find a way to emulate the keypad and I cant do that if the data is wrong.
I can send it back to the manufacturer to be reset but what fun is that hey? Would rather open up some possibilities for home automation. Any ideas would be most appreciated.
Next the code writes "48" to pin 3
All that does is set a duty cycle of 48/255 - is that what you intended?
Im afraid I dont know what a duty cycle is but what I really want to do is place my arduino between my alarm keypad and the control pannel. I want the code for the alarm. They say a picture is worth 1000 words, so here's 2000.
pos, neg, data, clk
A duty cycle is the ratio between on and off.
Then yes, that is what I intended but although I put 48 in, I did not get 48 back. This means when I connect the alarm keypad to pin 3, it will be wrong.
Does the panel really work like that with PWM?
I've never heard of encoding stuff like that.
I really don't know, im just guessing. Its an old alarm so it wont be anything special, we're talking serial. Problem is that I don't know how to read it. If I simply read the analog pin with analog read I only get voltages. So, i've been trying anything I can till I get something I can make sense of and this was the first thing that made any sense since I found what appears to be stop bits.
I'd give up on guessing and tackle it with an oscilloscope.
Yep, don't have one. Surely there cant be that many different ways that were utilized in 1989. I mean its not going to be bluetooth.
I mean its not going to be bluetooth.
It's also unlikely to be PWM.
