Show Posts
Pages: [1]
1  Forum 2005-2010 (read only) / Syntax & Programs / Re: Serial - Interrupt on: October 30, 2010, 05:39:27 am
Sorry to reply to this topic two years later, but this is the first result I get by Google to the query "Arduino serial interrupt".
I see that someone solved this problem and even provided a library to handle Serial I/O via interrupts. In one case an extension to HardwareSerial provided with Arduino has been written.

You can find that here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1191505972
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1265509976/3
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1242292909/2

I don't know if this helps, but I just succeeded in doing that in a very simple way.
Maybe it uses the same priciples used by the above solutions, but I don't know, since the provided code is quite complex to me.

My humble solution is to reenable interrupts while in the interrupt service routine to allow Serial to work.
Then wait for data in the Serial data to be available.

Drawback: this steals a bit of CPU when receiving data: the ISR activates on the Start bit of the serial data. Then it has to wait for the 10 bits on 8N1 protocol to be received. At 9600bps this means wasting about 1/960 seconds, i.e. about 16.000 cpu cycles.
The worst thing is that if multiple bytes are sent without any rest, the ISR will be stealing the CPU for about 1/960 seconds and then leave it free for 1/9600, the mere time between the end of a byte and the biginning of the next one, and then continue cycling like this.

So my approach is not suitable for every application.
A solution to this problem might be to disasbile interrupts and reenable them in the main loop, using noInterrupts() and interrupts() functions, reducing the number of ISR calls during main loop.

This is my humble code:
Code:
// To use this example, you have to connect Rx pin (digital pin 0) to interrupt 0 pin (digital pin 2).
void setup()
{
    // Using interrupt 0 on digital pin 2.
    pinMode(2, INPUT);
    digitalWrite(2, LOW);
    
    Serial.begin(9600);
    attachInterrupt(0, serialInterrupt, CHANGE);

    // Used to signal that main loop is alive.
    pinMode(4, OUTPUT);
    digitalWrite(4, LOW);

    // Used to signal that Serial input was read.
    pinMode(5, OUTPUT);
    digitalWrite(5, LOW);
}

void loop()
{
  // Do something using even delays. There is an interrupt for that (Serial I/O)!

  // Blink led to signal loop is alive.
  digitalWrite(4, HIGH);
  delay(500);
  
  digitalWrite(4, LOW);
  delay(500);
}

// Volatile, since it is modified in an ISR.
volatile boolean inService = false;

void serialInterrupt()
{
  // Trick: since Serial I/O in interrupt driven, we must reenable interrupts while in this Interrupt Service Routine.
  // But doing so will cause the routine to be called nestedly, causing problems.
  // So we mark we are already in service.

  // Already in service? Do nothing.
  if (inService) return;

  // You was not in service. Now you are.
  inService = true;
  
  // Reenable interrupts, to allow Serial to work. We do this only if inService is false.
  interrupts();
  
  // Allow serial to read at least one byte.
  while(!Serial.available());

  // Blink led to signal Serial data arrived.
  digitalWrite(5, !digitalRead(5));
  byte data = Serial.read();

  // Echo data back to developer ;-)
  Serial.print(data);

  // Job done.
  inService = false;
}

I  tried this other approach, detaching and reattaching ISR, but it stops the main loop. Do know why.

Code:
void serialInterrupt()
{
  detachInterrupt(0);
  interrupts();
  
  while(!Serial.available());

  digitalWrite(5, !digitalRead(5));
  byte data = Serial.read();
  Serial.print(data);

  attachInterrupt(0, serialInterrupt, CHANGE);
}
2  Forum 2005-2010 (read only) / Interfacing / Re: Does a Magnetic Contact RFID sensor exist? on: September 27, 2010, 10:12:44 am
Code:
Optical. Put a little reflector on the door so that when the door is closed, it reflects a laser beam back to your receiver.  Of course, it will be very much more expensive and fiddly than just running wires or replacing a battery every 6 months. Now you know why wires are popular even though perhaps inconvenient.
Really interesting. In my case, the doors to check are at the left and the right of the point where the main unit is, at a distance of about 1 meter each one. So it would be entirely possibile.
I can just use a photocell like the ones used in gate automation and a reflective surface or sticker on the doors!

Thank you very much!  smiley-wink

I am still interested in the RFID solution, because I would learn something about this technology and this would be a chance.
3  Forum 2005-2010 (read only) / Interfacing / Re: Does a Magnetic Contact RFID sensor exist? on: September 27, 2010, 10:07:58 am
Thank you for the response.

Quote
Do you mean an RFID tag that can read a magnetic contact switch and report back the state of that switch.
You got it!

Quote
If so I don't think one exists but it would be easy enough to make.
How could I do that? Can I Build an RFID tag by myself?
I thought to take an existing tag and break one of its circuits (to be determined: which circuit) and connect it to a common magnetic sensor. Could it work?

Quote
However, you are not going to get a 2 meter range from a passive RFID tag of the sort that you could make yourself.
I read that RFID passive tags can cover 20 feets [edit](6 meters)[/edit] (http://electronics.howstuffworks.com/gadgets/high-tech-gadgets/rfid3.htm). This is absolutely an indicative measure.
It would be nice to identity a tag/gayeway cuople that  covers 2 meters (and is Arduino-compatible).
4  Forum 2005-2010 (read only) / Interfacing / Does a Magnetic Contact RFID sensor exist? on: September 27, 2010, 08:25:54 am
[glow]I am looking for a sensor to check when a door is closed that must be wireless and possibly batteryless.[/glow]
Normally such check is performed by a magnetic contact (the ones used for antitheft systems).
I would avoid using cables to connect the sensor to the main board, that will be at most 2 meters away from it.

So I was thinking about an RFID application. An RFID tag that has a magnetic contact would be perfect.
But I cannot find such thing on the Internet and I am unsure it even exists.

I heard that there are RFID tags that performs more than just sending their IDs, but can store information and send it as well. But I found only ID tags.

I know about EnOcean products. They are very interesting (wireless, batteryless and with a RF range of 300 meters!)
But I am looking for samething simpler and cheaper.

Here are some of them:
http://uk.rs-online.com/web/search/searchBrowseAction.html?method=getProduct&R=0189188
http://uk.rs-online.com/web/search/searchBrowseAction.html?method=getProduct&R=0189009&cm_vc=prev_uk

[glow]Do anybody know about wireless and batteryless contact sensors?[/glow]

Thank you for any help.
5  Forum 2005-2010 (read only) / Bar Sport / Re: Netduino on: September 20, 2010, 04:15:26 am
I have been using Arduino only for three weeks and I found it incredibly simple to use (I finished my project using Ethernet shield, UDP protocol, two gas sensors and an IR sensor attached to it, in the spare time of less than two weeks).

Yesterday I noticed Netduino and I am very impressed!
The computational power of Netduino fairly outperforms Arduino. I am a C# developer, and so I really appreciate Netduino.
The thing that shocked me is that Netduino costs only 8€ more than Arduino (30.96€ from coolcomponents).

Now I am very undecided: shoud I leave Arduino alone, and dedicate myself only to development with Netduino?
I am wondering what are Arduino advantages over Netduino.

Maybe the cost, since an AtMega328 with Arduino bootloader costs as low as 4.80€ (the lowest price I found at the moment), while I can't still find an AT91SAM7X512 with bootloader alone.

Another point is that the AT91SAM7X512 requires SMD soldering, while AtMegs328 not.

My question is : are there applications where Arduino is best suited?
Pages: [1]