Arduino之坑(零)——digitalWrite与servo.write

0.参考链接:

https://forum.arduino.cc/index.php?topic=123174.0

1.问题答案

今天用digitalWrite给电调信号,发现难以带动同事的吸气泵,然后我和同事联系了客服,发现利用servo.write函数是没问题的,能够正常工作。

看到老外的解释是digitalWrite() 仅仅表示打开或者关闭引脚,analogWrite()能进行告诉的开关闭信号,也就是说analogWrite()函数更适合拿来做PWM波

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个项目需要使用OpenMV相机来进行图像处理,以便检测停车位。然后,使用Arduino控制小车的运动,以便将其移动到停车位。 以下是一个简单的代码示例,用于检测停车位并将小车移动到该位置: 首先,需要在OpenMV上运行以下代码,以检测停车位并确定小车应该向左还是向右移动: ```python import sensor, image, time # 设置相机 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # 必须关闭自动增益 sensor.set_auto_whitebal(False) # 必须关闭自动白平衡 # 设置图像区域 x_min = 0 y_min = 100 x_max = 319 y_max = 220 while(True): img = sensor.snapshot() # 仅在图像的指定区域内进行处理 img = img.crop(roi=(x_min, y_min, x_max-x_min, y_max-y_min)) # 将图像转换为灰度,并进行二值化处理 img = img.to_grayscale() img = img.binary([THRESHOLD]) # 在图像中查找停车位 spots = img.find_rects() # 如果没有停车位,则向前行驶 if not spots: # 向前行驶的代码 pass # 如果有停车位,则向左或向右移动 else: # 查找停车位的中心点 center_x = spots[0].cx() # 如果中心点在左侧,则向左移动 if center_x < (x_max - x_min) / 2: # 向左移动的代码 pass # 如果中心点在右侧,则向右移动 else: # 向右移动的代码 pass ``` 接下来,需要在Arduino上运行以下代码,以控制小车的运动: ```c++ #include <Servo.h> // 设置舵机 Servo servo; int servoPin = 9; // 设置电机 int motorPin1 = 5; int motorPin2 = 6; int motorPin3 = 10; int motorPin4 = 11; int speedPin = 3; void setup() { // 初始化舵机 servo.attach(servoPin); // 初始化电机 pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(motorPin3, OUTPUT); pinMode(motorPin4, OUTPUT); pinMode(speedPin, OUTPUT); } void loop() { // 向前行驶的代码 digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW); analogWrite(speedPin, 255); // 向左转的代码 servo.write(0); // 向右转的代码 servo.write(180); } ``` 将这两个代码合并在一起,就可以实现自动泊车小车的功能了。需要注意的是,这只是一个简单的示例代码,需要根据实际情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值