blink and jizz loop

fukurou

the supreme coder
ADMIN
the Arduino only blinks the LED when it receives a command from the Python script.

Arduino Code​

This code will wait for a command from the Python script to blink the LED:
C++:
void setup() {
  // Initialize the built-in LED pin as an output
  pinMode(LED_BUILTIN, OUTPUT);
  // Start serial communication at 9600 baud
  Serial.begin(9600);
}

void loop() {
  // Check if data is available to read
  if (Serial.available() > 0) {
    // Read the incoming byte
    char data = Serial.read();
    // Blink the LED if the received data is '1'
    if (data == '1') {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000); // LED on for 1 second
      digitalWrite(LED_BUILTIN, LOW);
      delay(1000); // LED off for 1 second
    }
  }
}


Python Code​

This Python script will send a command to the Arduino to blink the LED:
  1. Install pyserial (if not already installed):
    pip install pyserial

  2. Python script:
  3. Python:
    import serial
    import time
    
    # Set up the serial connection with a timeout of 0.1 seconds
    arduino = serial.Serial('COM3', 9600, timeout=0.1)
    time.sleep(2)  # Wait for the connection to establish
    
    try:
        while True:
            # Send the command to blink the LED
            arduino.write(b'1')
            print("Command sent to blink LED")
            time.sleep(2)  # Wait for 2 seconds before sending the next command
    except KeyboardInterrupt:
        print("Exiting the loop and closing the connection.")
    finally:
        arduino.close()
        print("Serial connection closed.")

Explanation​

  • Arduino Code: The Arduino code waits for a serial input. When it receives ‘1’, it blinks the LED on and off for one second each.
  • Python Code: The Python script sends the ‘1’ command to the Arduino every 2 seconds, which triggers the LED to blink.
This setup ensures that the Arduino only blinks the LED when it receives the command from the Python script. Adjust the serial port in the Python script to match the one your Arduino is connected to.
 
Last edited:

fukurou

the supreme coder
ADMIN
.h
C++:
#ifndef DiBlinker_H
#define DiBlinker_H
// using Arduino hardware codes outside main:
#include <Arduino.h>
#include "LivinGrimoireLight.h"
#include "DiHelloWorld.h"

// example hello world by blinking default Led #13 once
class DiBlinker : public Skill {
    private:
    Led _l1;
    public:
    DiBlinker(Led l1);
    virtual void inOut(byte ear, byte skin, byte eye);
};
#endif

.cpp
C++:
#include <Arduino.h>
#include "DiBlinker.h"
#include "LivinGrimoireLight.h"
#include "DiHelloWorld.h"

DiBlinker::DiBlinker(Led l1)
{
   Serial.begin(9600);
  _l1 = l1;_l1.init();
}
void DiBlinker::inOut(byte ear, byte skin, byte eye)
{
  // Check if data is available to read
  if (Serial.available() > 0) {
    // Read the incoming byte
    char data = Serial.read();
    // Blink the LED if the received data is '1'
    if (data == '1') {
      // turn the LED on (HIGH is the voltage level)
      _l1.on();
      delay(1000); // Wait for 1000 millisecond(s)
      // turn the LED off by making the voltage LOW
      _l1.off();
      delay(1000); // Wait for 1000 millisecond(s)
    }
  }
}

main:

C++:
#include "DiBlinker.h"
#include "LivinGrimoireLight.h"
Chobit* c1 = new Chobit();
void setup() {
    Led led1(13); // used to initialize the Hello World skill
    Skill* s2 = new DiBlinker(led1); // example skill created   
    c1->addSkill(s2);
}

void loop() {
  c1->think(0, 0, 0);
}

skill python:

Python:
class DiBlinker(Skill):
    def __init__(self):
        super().__init__()

    # Override
    def input(self, ear: str, skin: str, eye: str):
        if ear == "blink":
            self.setVerbatimAlg(4, "blinking")
            ser.write(b'1')
 
Top