Go Down

Topic: Hi Guys please help me with code, Really urgent. (Read 1 time) previous topic - next topic

ArdGuy

Jan 01, 2013, 09:48 am Last Edit: Jan 01, 2013, 12:19 pm by ArdGuy Reason: 1
Hey Guys

I am in dire need of some code.Now its probably not advanced at all but I cant seem to wrap my head around it quickly enough

This is from another post that I already asked the question.

Quote

Hey Guys
Im relatively new to Arduino and I got asked to build a machine that reacts on Pulses received.I am using the Arduino NANO.
Trick is when ever a pulse is received it gets locked into a piece of code roughly 4 hours until fully executed.
Now the 1 interrupt pin also serves as the pin that gets the pulse.
If the Pin gets the Pulse it switches on the long duration of code, my Problem comes when I need it to stop with whatever its doing immediately by PULSING it again from the same source onto the same Pin
that started the 4 hour code.

So basically if it gets a Pulse it starts but at any given time I want to be able to pulse it again and it should stop or "Break;" the code its busy with and just exit on the Same pin.
The reason Im asking is because I cant seem to use any functions inside an Interrupt with Delays in them and I have read why but now im clueless with ways to dodge this problem.

Thanks allot guys.
Ard


I have already compiled allot of code over the last 2 days with some success BUT its not stable enough for the project.Like the pulse that I give it with my hand's duration is to long and it gets the interrupt 3 times in a really short while.Almost not noticeable.

Here is the old post.

http://arduino.cc/forum/index.php/topic,140010.0.html

I am using the Arduino Nano.

PaulS

You don't need interrupts at all. In fact, all that the interrupt will tell loop() is that it needs to do something different. It is then up to loop() to notice that, and respond accordingly. Reading the state of the switch often enough is equally easy.

Simply locate every call to delay() and delete it.

Having done that, you'll probably notice that the 4 hour loop completes in a couple of milliseconds. If that is not what you want, you'll need to read, understand, and embrace the blink without delay example, and rewrite all of your code.
The art of getting good answers lies in asking good questions.

doulos24

#2
Feb 05, 2013, 08:47 am Last Edit: Feb 05, 2013, 03:34 pm by doulos24 Reason: 1
I know a dirty way to make it work call delay for 1ms and use a for counter to make up the rest of your delay time that way your only blocking the interrupt for 1 ms  

Quote
const int led = 13;
const int Op1 = 8;
const int Op2 = 9;
const int Op3 = 10;
const int Op4 = 11;

const int int1 = 2; //Interrupt 0;

int lState = LOW;
int buttonState = 0;
long previousMillis = 0;
long inter = 1000;
//long UniTime = 10000;
int StateCheck =0;


void setup()
 {
   // digital pins=output
   pinMode(led, OUTPUT);
   pinMode(Op1, OUTPUT);
   pinMode(Op2, OUTPUT);
   pinMode(Op3, OUTPUT);
   pinMode(Op4, OUTPUT);
   pinMode(int1, INPUT);
   //attachInterrupt(0, stop1. HIGH);
   
 }
 
 void loop()
{
    buttonState = digitalRead(int1);
         
   if (buttonState == LOW)  {
         
    StateCheck =1;
    digitalWrite(Op1, HIGH);  
   for (int x=0;  x < 10000; x++){
       delay(1);  
       }          
      digitalWrite(Op1, LOW);    
   for (int x= 0; x < 1000;  x++){
      delay(1);              
       }
     digitalWrite(Op2, HIGH);  
     for(int x=0;  x < 10000; x++){
     delay(1);  
       }                              
      digitalWrite(Op2, LOW);    
      for (int x= 0; x < 1000;  x++){
      delay(1);  
       
        digitalWrite(Op3, HIGH);  
        for(int x=0;  x < 10000; x++){
        delay(1);  
        }                    
        digitalWrite(Op3, LOW);    
        for (int x= 0; x < 1000;  x++){
       delay(1);  
       }
        digitalWrite(Op4, HIGH);  
        for(int x=0;  x < 10000; x++){
       delay(1);  
       }                      
        digitalWrite(Op4, LOW);    
        for (int x= 0; x < 1000;  x++){
       delay(1);
       }
        StateCheck = 0;
 }
}
}


this will work but like I said its dirty and not the "right" way to do this. As long as your pulse change is longer then 1 ms you should be fine.

Go Up