Pages: [1]   Go Down
Author Topic: can I do serial output while in an interrupt-called function?  (Read 437 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a motorshield stacked on an ethernet shield stacked on a duemilanove arduino.  I have a switch connected to digital pin 13, which is set to high, with the other end of the switch connected to digital pin 2, with a resistor between pin 2 and ground.  I'm trying to use an interrupt to see when pin 2 goes high when the switch closes.  This seems to work ok, but I also wanted to write from the interrupt-called function that the switch has closed.  In the attached code, a motor (1) is started, then there is a loop to wait for the interrupt.  The motor starts, and stops when the switch is closed, but the serial println doesn't show up on the serial monitor.  Any ideas as to why this is the case?  Eventually I also wanted the stop function to post something to a client.  If I can't write from within the stop function I probably could monitor the status in the while loop instead...  Should I be able to do this serial output?  Maybe there are some pin conflicts that prevent the serial write when pin 2 is high?

Code:
#include <AFMotor.h>
#include <SPI.h>
AF_DCMotor motor(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
void setup() {
  Serial.begin(57600);
  motor.setSpeed(200);     // set the speed to 200/255
  // use interrupt 0 (pin 2 on duelimove)
  attachInterrupt(0, stop, RISING);
  Serial.println("starting motor");
  motor.run(FORWARD);
  pinMode(13, OUTPUT);
  digitalWrite(13,HIGH);
  //pinMode(2, INPUT);
}

void loop() {

}

void stop() {
  Serial.println("interrupt detected stopping motor");
  motor.run(RELEASE);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25795
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got to ask why you feel you need an interrupt.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's a good question.  I originally wanted to just do a loop after starting the motor until the switch closed, but the motor.run(FORWARD) command wasn't executing when I put any kind of loop after it.  When I took the loop out, it did execute.  So I tried the interrupt approach.  It actually does work to shut off the motor when the switch closes, but I don't get any output while in the interrupt function.  I reread the Arduino reference and it says serial data may be lost in the interrupt routine.  Thanks for bringing me back to what I originally was trying to do.  Here's the bit of code that turns the motor on and then is supposed to wait for the switch to close.  When the "while {" and closing "}" are commented out, the motor will turn on.  If I put the while loop back in, the motor doesn't turn on.

Code:
    //up
    Serial.println("opening door");
    delay(500);
    client.println("<p>Internet Door Controller<br />Waiting for door to open&nbsp;</p>");
    motor.run(FORWARD);

    //while (digitalRead(2) == LOW ){
            Serial.print("serial pin 2 = ");
            Serial.print(digitalRead(2));
            Serial.print("  serial pin 14 = ");
            Serial.print(digitalRead(14));
            Serial.print("\r");
    //}
   
Logged

Pages: [1]   Go Up
Jump to: