2

I am very new to Arduino and coding/programing. I set a task for myself to make a simple circuit of 5 LEDs controlled by pins 2, 4, 8, 11, 12. I want to be able to make two different pattern for the LEDs and be able to turn each pattern on or off with buttons in python. I was able to achieve this using millis and several if statements. I have one problem with the code I created and one question.

The problem: When I run my upload my code and press the buttons on my python gui only one task can run at a time. I would like to be able to run both at the same time if I press both run buttons. I have spent hours trying to find where my code is blocking this from being possible. Any advice on how to solve this problem would be greatly appreciated.

The question: During my research I stumbled across the possibility of using switch / case commands in stead of several if statements. I tried several times to get this to work but am not wise enough to get it to work. Any tips of how to make this work would be greatly appreciated.

Arduino code:

char serialData;

const int blue1 = 12; const int blue3 = 8; const int red1 = 4; const int red2 = 2;

int blue1state = LOW; int blue3state = LOW; int red1state = LOW; int red2state = LOW;

unsigned long previousMillis1 = 0; unsigned long previousMillis2 = 0;

const long interval1 = 0; const long interval2 = 2000; const long interval3 = 4000; const long interval4 = 3000; const long interval5 = 6000; const long interval6 = 9000;

void setup() { Serial.begin(9600); pinMode(blue1, OUTPUT); pinMode(11, OUTPUT); pinMode(blue3, OUTPUT); pinMode(red1, OUTPUT); pinMode(red2, OUTPUT); }

void loop() { unsigned long currentMillis = millis();

//TASK ONE if (Serial.available() > 0) { serialData = Serial.read(); Serial.print(serialData); }

if (serialData == '1') { if (currentMillis - previousMillis1 >= interval1) { red1state = HIGH; }

if (currentMillis - previousMillis1 >= interval2) {
  red1state = LOW;
  red2state = HIGH;
}

if (currentMillis - previousMillis1 >= interval3) {
  red2state = LOW;
  previousMillis1 = currentMillis;
}

}

if (serialData == '3') { red1state = LOW; red2state = LOW; }

//TASKTWO if (serialData == '2') { digitalWrite(11, HIGH);

if (currentMillis - previousMillis2 >= interval1) {
  blue3state = HIGH;
}

if (currentMillis - previousMillis2 >= interval4) {
  blue3state = LOW;
}

if (currentMillis - previousMillis2 >= interval5) {
  blue1state = HIGH;
}

if (currentMillis - previousMillis2 >= interval6) {
  blue1state = LOW;
  previousMillis2 = currentMillis;
}

} else if (serialData == '4') { blue1state = LOW; blue3state = LOW; digitalWrite(11, LOW); }

digitalWrite(blue1, blue1state); digitalWrite(blue3, blue3state); digitalWrite(red1, red1state); digitalWrite(red2, red2state); }

Python code:

import tkinter as tk
import serial
import time

arduinoData = serial.Serial('COM3',baudrate = 9600, timeout=1) def task_one(): arduinoData.write(b'1') def task_two(): arduinoData.write(b'2') def off_one(): arduinoData.write(b'3') def off_two(): arduinoData.write(b'4')

HEIGHT=700 WIDTH=800

LED_Control_Window = tk.Tk()

canvas = tk.Canvas(LED_Control_Window, height=HEIGHT, width=WIDTH) canvas.pack()

frame = tk.Frame(LED_Control_Window, bg='#80c1ff') frame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)

label = tk.Label(frame, text='LED Flash Patterns', bg='#beced4') label.place(relx=0.34, rely=0.0, relwidth= 0.35, relheight=0.1)

button = tk.Button(frame, text="Task One", bg='#50bd57', command=task_one) button.place(relx=.15, rely=0.25, relwidth= 0.25, relheight=0.15)

button = tk.Button(frame, text="Task Two", bg='#afbd3d', command=task_two) button.place(relx=0.62, rely=0.25, relwidth= 0.25, relheight=0.15)

button = tk.Button(frame, text="Stop Task One", bg='#f03535', command=off_one) button.place(relx=0.15, rely=0.6, relwidth= 0.25, relheight=0.15)

button = tk.Button(frame, text="Stop Task Two", bg='#f03535', command=off_two) button.place(relx=0.62, rely=0.6, relwidth= 0.25, relheight=0.15)

LED_Control_Window.mainloop()

timemage
  • 5,639
  • 1
  • 14
  • 25

0 Answers0