Show Posts
Pages: 1 [2] 3 4 ... 18
16  Using Arduino / Programming Questions / Re: Using the RDM630 RFID Tag Reader and NewSoftwareSerial library on: November 01, 2012, 10:03:04 am
I don't want to start a flame war here, but there are some rare occasions where using gotos is justified (OP's example was obviously not one of those). I have sometimes used gotos as a kind of a exception throwing mechanism where it makes handing error conditions  simpler. Something like:

Code:
int foo()
{
    int ret;
    if (!(ret = do_something())) goto err;
    
    ...

    if (!(ret = do_something_else())) goto err;
    if (!(ret = do_something_more())) goto err;

    return OK;

  err:
    rollback();
    show_error_message();
    return ERR;
}


17  Using Arduino / Programming Questions / Re: How to read from serial in c ? (under linux) on: November 01, 2012, 09:43:38 am
Tried with a normal user space program and still does not work: a lot of strange characters come out.
I see a problem in your code too: you do not set baud rate at all, I think this has to be a problem...

Yes, you obviously have to set the baud rate and possibly other tty parameters. You can use stty from the command line before running the program or cfsetspeed function in your program.
18  Using Arduino / Programming Questions / Re: How to read from serial in c ? (under linux) on: October 29, 2012, 07:25:34 am
If you are sending integers only, it may be easiest to send them as binary data,  though there are still some quirks you have to take care of:
- Check if the Arduino and your Linux computer share the same byte order (little endian vs. big endian). If not, you need to convert to/from the network byte order when sending/receiving the integers. (Check htons/ntohs man pages on Linux).
- "int" is 16 bits on the Arduino and probably 32 bits on your Linux computer.  Use "short" on Linux instead.
19  Using Arduino / General Electronics / Re: Rotary Switch Colour Selector help needed on: October 23, 2012, 02:32:27 pm
Well from a programming stand point, I think a seven level deep block of nested if-then-else statements evaluating if each pin is TRUE would be fairly simple.  However, it would take up quite a few lines by itself and be somewhat cumbersome if you ever had to debug it. smiley-wink  Furthermore it's perhaps the most compact ways to express the logic without getting into rather arcane tricks.

Why on earth would it need to be anything that complicated? A simple loop through the pins is enough, assuming he uses subsequent (is this the right word?) pins.

Code:
byte colorNum = 0;
for (byte pin=2; pin <= 8; pin++) {
    if (digitalRead(pin) == LOW) {
      colorNum = pin;
      break;
   }
}
if (colorNum) color = colorMap[colorNum];
20  Using Arduino / General Electronics / Re: Rotary Switch Colour Selector help needed on: October 23, 2012, 02:15:09 pm

I don't think a rotary switch is as easy as you think... it would need quite a lot of code and I bet there's no library for it.

A rotary encoder library with examples is probably easier for a novice to get working.


I don't think a rotary switch is as complicated as you think  smiley-wink I already provided guidelines for the sketch in reply #1. If the OP has any programming experience at all, he will probably write the code in the time it would take to find a suitable library for a rotary encoder.
(edit: typo)
21  Using Arduino / General Electronics / Re: Rotary Switch Colour Selector help needed on: October 23, 2012, 12:05:28 pm
I believe it is a bit more complicated to read gray code/ rotary encoder. But perhaps there are Arduino libraries for that, though.
Yes, there is a whole section of the Playground with different examples of how to use rotary encoders with Arduinos.

Yes, but it would still be more complicated than using a rotary switch with each pin connected a different input pin. If this is Kobra299's fist Arduino project, I suggest going with the simpler alternative.
22  Using Arduino / General Electronics / Re: Rotary Switch Colour Selector help needed on: October 23, 2012, 11:47:54 am
A switch like that needs an awful lot of Arduino pins to connect it (12 of them).

He only needs to connect 7 pins. He can leave the rest of the pins disconnected.

If pins are important you'd be much better off with a "rotary encoder". This will do the same thing but only needs two pins for the shaft rotation. You also get a built-in push button (push the shaft down) which you can connect to a third pin.

I believe it is a bit more complicated to read gray code/ rotary encoder. But perhaps there are Arduino libraries for that, though.
23  Using Arduino / General Electronics / Re: Rotary Switch Colour Selector help needed on: October 17, 2012, 01:51:51 pm
You will need more than one or two pins.

One of the switch's pins is the center pole (or common terminal or something like that, I'm not sure what the correct English term is). When you rotate the switch, it connects the center pole with one of the 12 other terminals. Connect the center pole to the Arduino's ground and the other terminals (as many of them as you need) to different digital input pins, for example pins 2 to 8. In you sketch, put the pins to the input-mode and enable the internal pull-up resistors. Associate the colors with different pins, for example 2=red, 3=green, ... 8=random. Read the pin values. The pin that is LOW is the one that is selected.

24  Using Arduino / Programming Questions / Re: On one line on: October 15, 2012, 06:40:24 am
Yes, I totally agree with with you, Nick and  bperrybap. I didn't claim that direct port manipulation (or using "while" instead of "for") was a better way to do it. I just meant to demonstrate that there are several ways to do it.
25  Using Arduino / Programming Questions / Re: VirtualWire send multiple variables at once on: October 14, 2012, 03:01:32 pm
It would be more efficent to send the data in binary form:

Code:
  const char * timestamp = "2012.10.13";
  float voltage = 4.59;
  float current = 0.36;
  float power = 1.65;

  int size_of_float = sizeof(float);
  int size_of_timestamp = strlen(timestamp);

  uint8_t buffer[size_of_timestamp+size_of_float*3];
  void *p = buffer;
  memcpy(p, (void *)timestamp, size_of_timestamp);
  p += size_of_timestamp;
  memcpy(p, (void *)&voltage, size_of_float);
  p += size_of_float;
  memcpy(p, (void *)&current, size_of_float);
  p += size_of_float;
  memcpy(p, (void *)&power, size_of_float);

  vw_send(buffer, sizeof(buffer));


And you could also send the timestamp as a 32bit integer instead of a string.
26  Using Arduino / Programming Questions / Re: On one line on: October 14, 2012, 04:06:18 am
You can also use direct port manipulation without fors or whiles:

Code:
DDRD = DDRD | B0111111;  // pins 2-7
DDRB = DDRB | B10000000; // pin 8
27  Using Arduino / Programming Questions / Re: On one line on: October 14, 2012, 03:52:58 am
All right already! The big bad FOR shows it head again!
Thought I could do it with these things:  (   ),    [   ],    {   }
But, guess not.
Thanks all... smiley

If you hate "for", use "while" instead:

Code:
byte i=2; while (i<=10) pinMode(i++,OUTPUT);
28  Using Arduino / Programming Questions / Re: How do you get the arduino to send an email notification on: July 12, 2012, 02:10:11 pm
First you need to figure out which authentication/encryption method your mail server requires. Chances are that the code zeus2kx posted wont work for you e.g. if your server requires TSL. If your server supports plain authentication you can use zeus2kx code as a template for your implementation. Before writing code for the Arduino try test the stmp communication from a PC using telnet. On Windows start the command prompt and telnet to the the smtp(25) port of your mail server:

Code:
C:\> telnet stmp.somewhere.net 25

When the telnet connection is established, type the stmp commands that send the message. The following commands work when the server do not require authentication (for example, if you send a message to an email address that is hosted locally on that server).

Code:
EHLO somewhere.net
mail from: <me@somewhere.net>
rcpt to: <joe@somewhere.net>
data
Subject: test

the body of the message.
.

You will see that the server writes back when you type the commands. For example, after you have typed the "mail from: <me@somewhere.net>" it may respond with "<me@somewhere.net>... Sender ok".

When you have successfully sent an email from your PC just transfer the command to client.print()-lines for the Arduino. It should work. On the other hand, if you can't send a message from a PC sending  email won't work from the Arduino either.
29  Using Arduino / Networking, Protocols, and Devices / Re: GET - http request not working? on: July 11, 2012, 04:27:35 pm
below is the complete code. I have made a new sketch with the air pressure value only - just to keep it simple. It's still not working.
I get the correct reading / values at the serial interface - only the GET request is not working.

Quote
How have you verified that the request is being cut? Did you check the server logs or did you use a network monitor/sniffer?
if you have this complete line in the code, it becomes a hyperlink and opens at the webbrowser but everything was cut off behind & - see my first post.
client.print("GET http://192.168.178.21/config/xmlapi/statechange.cgi?ise_id=15324&new_value=1005 HTTP/1.0");

Is there any way to monitor the GET Request in order to see what the arduino is send to the server?

Yes, easiest is to check the server's access log. It should show the GET request that the Arduino has sent. Or if for some reason you can't read the access log, you can use a network monitor (i.e. a sniffer) such as wireshark. You can't run the network monitor on the Arduino but you may be able to run it on the web server (if you have required privileges) or on some other network element on the route (on a firewall, router etc).

But did you test the request using telnet as I suggested? Had you done that you would have immediately noticed that the server replies with redirect message (probably with http status code 302 or 303). As I said before and PaulS repeated, testing with the browsers is not the same as sending the request from the Arduino. The browser may do several things behind the curtain that you may not be aware of, such as handles the redirects.

30  Using Arduino / Programming Questions / Re: Please modify this code on: July 10, 2012, 02:40:06 am
yes in this case its more handy to write it like that, less typing is better.

But suppose you wanted a more complex function to create a complex string for HTML formatting.
Based on the value of A the text color is red or green, and if A is below or above a treshold it should be bold too.
Things like that require longer peaces of code.

Yes, that is a better example where functions should be used. I just posted my comment because I didn't want udeshplus to think that he (or she) should actually implement the ConvertStringToInt function you presented. ConvertStringToInt wont simplify the code a bit and it brings in a lot of overhead. For example, you passed the param "s" as value. It means that calling the function will allocate a new String object from stack and execute its copy constructor.

Quote
As i am not sure how good you are in coding, i just noted it could also be written as your function, with a name you like to add to it.
If your learning c++ from other languages, it might be confusing to see how functions are made.
Without words like "Function" , well c++ is a bit barebones syntax , compared to languages like python  or java or basic, or ...etc

Essentially   int() is a build in function for arduino its allready there.

int() is actually not a function but a build in type conversion (or cast). Its equivalent of:
Code:
A = (int) lux_v1;
If lux_v1 is an instance of the String class, it will invoke  String's cast operator.


Pages: 1 [2] 3 4 ... 18