gpt4 book ai didi

python - 在windows中通过Arduino Uno执行python脚本

转载 作者:太空宇宙 更新时间:2023-11-03 16:08:29 26 4
gpt4 key购买 nike

我想知道有没有办法在 Windows 中通过 Arduino 命令运行 python 脚本?

最佳答案

我不知道这是否回答了你的问题,但你可以下载 Vpython 库来用它创建一些很酷的项目,或者连接传感器并从 arduino 将数据返回到 python 中,反之亦然

例如:

int trigPin=13; //Sensor Trig pin connected to Arduino pin 13
int echoPin=11; //Sensor Echo pin connected to Arduino pin 11
float pingTime; //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(trigPin, LOW); //Set trigger pin low
delayMicroseconds(2000); //Let signal settle
digitalWrite(trigPin, HIGH); //Set trigPin high
delayMicroseconds(15); //Delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); //Delay in low state

pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds
pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour
targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
targetDistance= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile)

Serial.println(targetDistance);

delay(100); //delay tenth of a second to slow things down a little.
}

在Python中

import serial #Import Serial Library
from visual import * #Import all the vPython library

arduinoSerialData = serial.Serial('com11', 9600) #Create an object for the Serial port. Adjust 'com11' to whatever port your arduino is sending to.
measuringRod = cylinder( radius= .1, length=6, color=color.yellow, pos=(-3,-2,0))
lengthLabel = label(pos=(0,5,0), text='Target Distance is: ', box=false, height=30)
target=box(pos=(0,-.5,0), length=.2, width=3, height=3, color=color.green)
while (1==1): #Create a loop that continues to read and display the data
rate(20)#Tell vpython to run this loop 20 times a second
if (arduinoSerialData.inWaiting()>0): #Check to see if a data point is available on the serial port
myData = arduinoSerialData.readline() #Read the distance measure as a string
print myData #Print the measurement to confirm things are working
distance = float(myData) #convert reading to a floating point number
measuringRod.length=distance #Change the length of your measuring rod to your last measurement
target.pos=(-3+distance,-.5,0)
myLabel= 'Target Distance is: ' + myData #Create label by appending string myData to string
lengthLabel.text = myLabel #display updated myLabel on your graphic

这将使Python中的图形代表您在超声波传感器前持有的物体,并且您可以看到该物体实时移动

我从这个网站获取了代码:

Toptechboy

这是一个非常好的教程,如何将 arduino 连接到 python!而且非常简单

关于python - 在windows中通过Arduino Uno执行python脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39514309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com