Control a specific pin on the Arduino Uno board using pyserial
Question: 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 :

PYTHON part of the 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.
    }
    

ARDUINO part of the 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.
     }
    

# FOR A BETTER UNDERSTANDING :

I built up small codes to show the doubts i had :

CODE FOR THE ARDUINO IS :

    
    
    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()>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 FOR THE PYTHON FRONT IS :

    
    
    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"
    

===============================================================================

Now the questions I had :

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.

2) Is it possible to perform the operations as mentioned in the comments of
the blackout functions?

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 `serial.write` command.
Hence avoiding the `while(1)` loop after the `ser.isOpen()`.


Answer: Have a look at this:

<https://github.com/ajfisher/arduino-command-server>

It's something I pulled together on the Arduino side to issue arbitrary
commands like switch a pin high / low and set PWM levels etc. It works over
both serial and network though it's a touch buggy on the network side at the
moment.

To use it, put the code on your arduino then you just write a python script
(or any other language that can use a serial connection) to connect over the
serial connection and then tell it what you want to do eg DIGW 1 HIGH etc

Also have a look at: <https://github.com/ajfisher/arduino-django-visualiser>
which is where I use a variation of this library to control some LEDs based on
some things going on in Django - it's more heavily python based.

