Show Posts
Pages: 1 ... 59 60 [61] 62 63 ... 225
901  Using Arduino / Programming Questions / Re: Binary Communication - Command Request Question on: July 11, 2013, 03:59:46 pm
you get 8 bytes? or 8 bits? { in ascii is 7B, so it will print 0111 1011 no? smiley-roll-sweat

Nope, you get 8 bytes:

'0', '1', '1', '1', '1', '0', '1', 1'

Note the single quotes. The BIN argument tells it to use the ASCII encoding of the binary value of the given character. '0' and 0 binary are not the same value, nor are the same bit-width:

'0' = 48 decimal = 0x30 = 0b00110000 // The character '0'
0 = 0 decimal = 0x0 = 0b0

What i basically was trying to do was to send
0111 1011 0111 1011 1101 0010 ... ....
  7     B       7     B      D      2 .... ....

As I suspected, you don't want to send 'D', '2', you want to send 0XD2. You can do something like this:

Serial.write('{'); // sends 0x7B
Serial.write('{'); // sends 0x7B
Serial.write(0xD2); // sends 0xD2
Serial.write(0x00); // sends 0x00
Serial.write(0x02); // sends 0x02
Serial.write(0xD4); // sends 0xD4
Serial.write('}'); // sends 0x7D
Serial.write('}'); // sends 0x7D

Both print and write still send binary data. The different is how they interpret the data to send. If the data provided is numeric, print will convert it to the ASCII representation which could be up to 3 bytes worth of data. write will just send the single byte of binary data:

Serial.print(2); // sends '2' = 0b01010000
Serial.write(2); // sends 2 = 0b00000010

For the former block of code, we can save a couple of lines of code by using an array:

byte command[] = {
    {', '{', 0xD2, 0x00, 0x02, 0xD4, '}', '}'
Serial.write(command, sizeof(command));
902  Using Arduino / Programming Questions / Re: Just plain don't undrestand on: July 11, 2013, 03:44:20 pm
Thanks all for the help. Rob, thank you for the corrections .The program compiled but didn't work. I gave up and used one from the tutorials that turned on an led using a switch. It works fine.

This exercise is for monitoring my AC usage. The relay turns on when the AC does and I want to write the output from the Arduino to Visual Basic and keep track of on and off times. That will be the next step.
I'm not sure if I need to use static outputs. That is, not continuously write an on or off verses do it once when the relay is closed or once when open. I may try to use "switch-case" if I need to.

So on to more learning.

Again, much thanks.

Sounds like you're looking to detect the signal edge. The StateChangeExample demonstrates how this is accomplished.
903  Using Arduino / Programming Questions / Re: Putting characters in a variable to send over serial on: July 11, 2013, 01:54:52 pm
Yes that's true... But my problem is that I don't get it how I can change the variable during the loop.

You mean like this?

void loop()
// code
someVariable = someNewValue;
// some more code
904  Using Arduino / Programming Questions / Re: Debounce on: July 11, 2013, 01:53:44 pm
Yesterday i spent several hours trying to understand that code, and experimenting, and do you have a better example?

I don't think it can get "better" than 6 lines of code.
905  Using Arduino / Programming Questions / Re: Debounce on: July 11, 2013, 01:38:24 pm
That's what i'm trying to do......

Well, it's certainly going to take more than 7 minutes.

That's only part of it though, you still need to incorporate the edge detection.
906  Using Arduino / Programming Questions / Re: Debounce on: July 11, 2013, 01:34:50 pm
I've seen that one, but i don't understand it

Sounds like its a good opportunity to take the time to learn how it works.
907  Using Arduino / Programming Questions / Re: Binary Communication - Command Request Question on: July 11, 2013, 01:25:43 pm
I think most of your confusion can be attributed to a misunderstanding as to how data is stored/sent/represented.

As I was told the ECU only understands binary data

All devices only understand binary.

But how is the data sent out anyway?

In a stream of HIGHs and LOWs.

And that was why i was sending in BIN

It already sends it as binary (A stream of HIGHS and LOWs). When you print using BIN as the second argument, you are telling the microcontroller: "Take this single byte, 8 bit piece of data, convert it to a combination of 8 '1's and '0's, and send those 8 bytes of data over the serial port.

So i should never use println when sending a command?

Not necessarily. Using println works if the device you are sending to expects a new line after each command. Often times, I will use '\n' as a terminating character when communicating over UART with two microcontrollers.

Regarding the delay, I'm not sure about how long the device takes to answer, Just know that it doesnt work instantly.

Then I wouldn't use delay() as it is poor practice. Eventually, you'll want to build a state machine that handles the communications. This will give you the added advantage of being non-blocking. For now though, I would use a bigger delay (like a whole second) until you can reliably get the responses you are expecting.

Running with the changes that you kindly suggested,

When you make changes to the code, it's a good idea to post the updated code using the proper CODE tags.

how should I save it?

Depends on how you want to use it.

Nevertheless, I should be getting consistent data, instead of

Since I don't have the datasheet, I couldn't say why you are getting those values. Are you certain it expects a 12 byte command? D20002D4 can also be interpreted as HEX, which would make it an 8 byte command.
908  Using Arduino / Programming Questions / Re: Reset Button Simple Programming on: July 11, 2013, 12:42:46 pm
I'm a little confused

I bet I'm more confused then you are...

have the button as "HIGH"

What button? Button's are input devices, so if you want it HIGH, don't press it (or do, depending on how it's wired).

first TTL is received

How does one "receive a TTL"?

How would I go about the LOW statement then

You're missing a verb in this statement

TTL pulses coming through

What do you consider a "TTL pulse"?

Perhaps take a step back and try to better explain what you are trying to accomplish in a broader sense, not how you are trying to accomplish it.
909  Using Arduino / Project Guidance / Re: Recalibration of Anolog input on: July 11, 2013, 12:27:34 pm
I am trying to get a reading from a potentiometer(0-5000) on the serial monitor and then be able to zero and recalibrate at different points with an IR remote at different points(say the value is 2000, I need to make that 0 if I hit a button and go from there). Thank you in advance, any help is much appreciated!

Ok, you have our permission...

..or did you have an actual question?
910  Using Arduino / Programming Questions / Re: Reset Button Simple Programming on: July 11, 2013, 12:24:01 pm
(I'm confused by what it means for "bring this line LOW"):

It means connect it to ground.

boolean status_button = HIGH
If (status_button == LOW)

You just set status_button to HIGH, how will that if statement ever be true?
911  Using Arduino / Programming Questions / Re: Binary Communication - Command Request Question on: July 11, 2013, 12:16:06 pm
I'll go ahead and remove the commented out code, clean up the formatting and post the code with the proper tags:

byte c;
String inputString="";

void setup()

  // initialize serial:


  Serial.print("Answer: ");

void loop()

Lets start here:


Those 12 characters can't fit into a single character, so you shouldn't be using single quotes. Furthermore, why are you using Serial.print with BIN? Does the device you are communicating with expect a stead stream of 48s and 49s (See the ASCII table)?  To expand on this, the following line of code:


will send out 8 bytes of data: a combination of '1's and '0's. This is only really for display purposes, there isn't much in this method for communication between devices.

You're also using println, so does the device expect a line feed ('\n') as well? This is where data sheets come in handy. Assuming you just want to print out that string, just use:


That will only send the 12 bytes that are contained within the string. Printing the entire string as binary would send 12*8 or 96 bytes, which I doubt was your intention.

Now on to this:


What kind of response are you expecting? Are you positive 10 ms is enough time to send 12 bytes, and expect X amount of bytes back at the 9600 baud rate?
912  Using Arduino / Programming Questions / Re: Binary Communication - Command Request Question on: July 11, 2013, 11:52:44 am
Regarding the code, i opted for not including, since I'm only trying to understand the basics of communication using binary data, and im not sure if my answers are making any sense, since they all seem random.

Then I'll opt not to respond anymore.

Seeing your code allows us to see what you're doing wrong and gives us hints as to what misconceptions you have. A common misconception about Serial data is that multi-byte responses all arrive simultaneously. Seeing your code would allow us to know if you've made that assumption.
913  Using Arduino / Programming Questions / Re: Debounce on: July 11, 2013, 11:50:36 am
Sorry, i mean that i am toggle a led when the button is pressed, but this code doesn't do anything, the led stays off.

So you need to combine the StateChangeDetection example with the Debounce example.

First get the Debounce example working. Add another (global or static) variable to is called lastButtonState and follow the concepts of the StateChangeDetection example for assigning and checking for the signal edge.
914  Using Arduino / Programming Questions / Re: Debounce on: July 11, 2013, 11:41:27 am
I am trying to toggle a led.

That's all?

digitalWrite(ledPin, !digitalRead(ledPin));

Perhaps you mean "I want to toggle an LED when..."
915  Using Arduino / Programming Questions / Re: Saving sensor readings on arduino on: July 11, 2013, 11:21:42 am
Get an SD card and shield; plenty of examples out there for this.

Alternatively, you could save it to be EEPROM, but this would be very limited in the amount you can save.
Pages: 1 ... 59 60 [61] 62 63 ... 225