Go Down

Topic: Arduino python 35 (Read 771 times) previous topic - next topic

a9balsza

Jul 21, 2016, 08:56 am Last Edit: Jul 21, 2016, 09:21 am by a9balsza
Hi!

I want to connect ardunio nano with python 35. i made a program in python that sends a character to arduino.If it's B then the LED must blink but it doesnt happen.


char cb;
char req;
bool echo = false;




ARDUINO SKETCH:
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
 
Serial.begin(9600);
 // initialize digital pin 13 as an output.
 pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
if (Serial.available()) {

   cb = Serial.read();

   if (echo) {
     Serial.write(cb);
     if (cb == 13) {
       Serial.println("");
     }
   }

   req += cb;

   // received LF, process command
   if (cb == 13) {

     if (req ='b') {      
 digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(250);
  digitalWrite(13, LOW);   // turn the LED on (HIGH is the voltage level)
  Serial.write(req);
 
}

   }    }    }






PYTHON SKETCH:



import sys
import serial
from tkinter import *
ser = serial.Serial('COM5', 9600,)

mGui = Tk()
mGui.geometry('450x450+500+500')
mGui.title('LED')
Mlabe= Label(text='LED').pack()



def  blink():
   ser.write(b'b')
   
POS=Button(text = "blink", command = blink).pack(side=LEFT)

AWOL

#1
Jul 21, 2016, 09:11 am Last Edit: Jul 21, 2016, 09:12 am by AWOL
Code: [Select]
char req;
...  if (req ="b") {


I'm a little surprised that even compiles.

Please use code tags when posting code.

a9balsza


AWOL


a9balsza



a9balsza

it compiles but when i push the button in python the led doesnt blink

AWOL

Have you fixed the other bug?

a9balsza


AWOL


a9balsza


AWOL


Robin2

#12
Jul 21, 2016, 10:33 am Last Edit: Jul 21, 2016, 10:34 am by Robin2
Have a look at this Python - Arduino demo
It may need a little modification for Python 3 as it was written with 2.7

Also see Serial Input Basics


I suspect your own Python program is sending data too frequently.

Also you should only send data when the button state changes. Perhaps send 'B' when the button goes down and 'b' when it is released.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up