title: Control a specific pin on the Arduino Uno board using pyserial
body: <p>I have a python code that sends in a pattern, in which a light has to blink in. (say eg. 101010. pattern may vary every time the code is run). when it is executing this infinitely i want an interrupt( again sent by the python code )to save the present conditions of the lights (say it is running 1 of the sequence) and perform a specific task like turn off the lights for 10 seconds and then resume the sequence. 
  one way of doing this is by interrupting the program by making the interrupt pin high. The question is can this making of high/low controlled by the pyserial.
  So a simple pseudo code would be :</p>
  
  <p>PYTHON part of the code: </p>
  
  <pre><code>Read the sequence:
  Send the sequence to the arduino board using pyserial.
  while(1)
  {
      Run a timer for 15 second.
      When the timer overflows interrupt the arduino.
  }
  </code></pre>
  
  <p>ARDUINO part of the code : </p>
  
  <pre><code>Read the sequence 
  while (1)
  {
      keep repeating the sequence on the LED.
  }
  
  // if interrupted on pin2  // assuming pin2 has the interrupt procedure
  // Pyserial has to enable the interrupt WITHOUT using a switch for enabling the pin.
  
  ISR 
  {
      Save the present state of execution.
      Turn off the LED.
   }
  </code></pre>
  
  <h1>FOR A BETTER UNDERSTANDING :</h1>
  
  <p>I built up small codes to show the doubts i had :</p>
  
  <p>CODE FOR THE ARDUINO IS :</p>
  
  <pre><code>int ledpin1 = 13;
  
  int speedy;
  
  int patterns;
  
  void setup()
  
  {
  
    Serial.begin(9600);
  
    Serial.print("Program Initiated: \n");
  
    pinMode(ledpin1,OUTPUT);
  
    //activate the blackout ISR when a interrupt is achieved at a certain pin. In this case pin2 of the arduino
  
    attachInterrupt(0,blackout,CHANGE);
  
  }
  
  void loop()
  
  {
  
    if (Serial.available()&gt;1)
  
    {
  
      Serial.print("starting loop \n");
  
      patterns = Serial.read();
  
      patterns = patterns-48;
  
      speedy = Serial.read();
  
      speedy = (speedy-48)*1000;
  
      while(1)
  
      {
  
        patterns = !(patterns);
  
        Serial.print(patterns);
  
        digitalWrite(ledpin1,patterns);
  
        delay(speedy);
  
      }
  
    }
  
  }
  
  /*
  
  void blackout()
  
  {
  
    // ***Save the present state of the LED(on pin13)***
  
    Serial.print ("The turning off LED's for performing the python code\n");
  
    digitalWrite(ledpin,LOW);
  
    //wait for the Python code to complete the task it wants to perform, 
  
    //so got to dealy the completion of the ISR
  
    delay(2000);// delay the whole thing by 2 seconds
  
    //***Continue with the while loop by setting the condition of the light to the saved condition.***
  
  }
  
  */
  </code></pre>
  
  <p>==================================================================================</p>
  
  <p>CODE FOR THE PYTHON FRONT IS :</p>
  
  <pre><code>import serial
  
  import time
  
  patterns=1
  
  speedy=1
  
  ser = serial.Serial()
  
  ser.setPort("COM4")
  
  ser.baudrate = 9600
  
  ser.open()
  
  def main():
  
      if (ser.isOpen()):
  
          #while(1):
  
          ser.write(patterns)
  
          ser.write(speedy)
  
          blackoutfunc()
  
          #ser.close()
  
  
  
  def blackoutfunc():
  
      while(1):
  
          time.sleep(2)
  
          print "Performing operations as required"
  </code></pre>
  
  <p>===============================================================================</p>
  
  <p>Now the questions I had :</p>
  
  <p>1) Is there a way to be able to activate the "blackout ISR" depending on the conditions of a pin(in this case pin2 which is the INT0 pin) without using a physical switch present on the pin. Hence the pin state has to be manipulated by the software.</p>
  
  <p>2) Is it possible to perform the operations as mentioned in the comments of the blackout functions? </p>
  
  <p>3)  In the python code is it possible to just send in the data(i.e. patterns,speedy) only once and make the arduino perform the pattern in a infinite way without again sending the data by the <code>serial.write</code> command. Hence avoiding the <code>while(1)</code> loop after the <code>ser.isOpen()</code>.</p>