メインコンテンツへスキップ
paste
bin
.ca
type · paste · share
⌘
K
ファミリー
bin ファミリー
pastebin.ca
ハブ
Share text and code with expiry and privacy controls.
imagebin.ca
Upload and share images with direct links.
filebin.ca
Drop a file and get a shareable link.
notebin.ca
Write Markdown notes with durable links.
turl.ca
Short, reputation-checked links.
attn.ca
Notifications and alerts for your services.
voicebin.ca
Record and share short voice clips.
dnsbin.ca
Inspect DNS and debug records.
ドキュメント
サインイン
?
← ペーストに戻る
›
編集 / フォーク
無題のペースト
#8k9dpaHY5Q
public / public
新しいバージョン
匿名
作成日 3 days ago
失効まで 4 days
3.3 KB
構文:
text
変更により、このペーストにリンクされた新しいペーストが作成されます — 元のペーストは変更されません。
新しいバージョン
変更により、このペーストにリンクされた新しいペーストが作成されます — 元のペーストは変更されません。
タイトル(任意)
ファイル名
構文
text
text
bash
c
cpp
css
diff
dockerfile
go
html
ini
java
javascript
json
kotlin
lua
makefile
markdown
nginx
php
python
ruby
rust
shellscript
sql
swift
toml
typescript
xml
yaml
可視性
パブリックフィード
アクセス
public
失効
7日
10分
1時間
1日
7日
30日
90日
カスタム…
カスタム失効
変更メモ
(任意)
このペーストはパブリックフィードに表示されます。リンクを知っている人だけに見せたい場合は可視性を変更してください。
新しいバージョンを作成
キャンセル
貼り付けるか入力…
/* ELEC1100 Your Lab#06 & Project Template To program the car tracking the white line on a dark mat Group No. (number of your project box): Group Member 1 (name & SID): Group Member 2 (name & SID): */ // assign meaningful names to those pins that will be used const int pinL_Sensor = A5; //pin A5: left sensor const int pinB_Sensor = A4; //pin A4: bumper sensor const int pinR_Sensor = A3; //pin A3: right sensor const int pinL_PWM = 9; //pin D9: left motor speed const int pinL_DIR = 10; //pin D10: left motor direction const int pinR_PWM = 11; //pin D11: right motor speed const int pinR_DIR = 12; //pin D12: right motor direction //define variables to be used in script int bumperSensor = 1; // not sensing white int leftSensor = 1; // not sensing white int rightSensor = 1; // not sensing white int countBumper = 0; // bumper sensor not triggered yet // the setup function runs once when you press reset or power the board void setup () { // define pins as input and output pinMode(pinB_Sensor, INPUT); pinMode(pinL_Sensor, INPUT); pinMode(pinR_Sensor, INPUT); pinMode(pinL_DIR, OUTPUT); pinMode(pinR_DIR, OUTPUT); pinMode(pinL_PWM, OUTPUT); pinMode(pinR_PWM, OUTPUT); // initialize output pins digitalWrite(pinL_DIR, HIGH); //forward direction digitalWrite(pinR_DIR, HIGH); //forward direction analogWrite(pinL_PWM, 0); //stop at the start position analogWrite(pinR_PWM, 0); //stop at the start position } // the loop function runs over and over again forever void loop() { // Arduino is reading the sensor measurements bumperSensor = digitalRead(pinB_Sensor); leftSensor = digitalRead(pinL_Sensor); rightSensor = digitalRead(pinR_Sensor); // car stops at the start position when bumper sensor no trigger if ( bumperSensor && countBumper == 0 ) { analogWrite(pinL_PWM, 0); analogWrite(pinR_PWM, 0); } // bumper sensor is triggered at the start position for the 1st time else if ( !bumperSensor && countBumper == 0) { analogWrite(pinL_PWM, 150); analogWrite(pinR_PWM, 150); countBumper = countBumper + 1; delay(350); //to let the car leave the start position with no miscount } // car is tracking on the white line else if ( bumperSensor && countBumper == 1) { if ( !leftSensor && !rightSensor ) { analogWrite(pinL_PWM, 180); analogWrite(pinR_PWM, 180); digitalWrite(pinL_DIR, HIGH); digitalWrite(pinR_DIR, HIGH); } // Case 2: Left sensor sees white, right sees dark -> Drifted right, steer left if ( !leftSensor && rightSensor ) { analogWrite(pinL_PWM, 50); analogWrite(pinR_PWM, 180); digitalWrite(pinL_DIR, HIGH); digitalWrite(pinR_DIR, HIGH); } // Case 3: Left sensor sees dark, right sees white -> Drifted left, steer right if ( leftSensor && !rightSensor ) { analogWrite(pinL_PWM, 180); analogWrite(pinR_PWM, 50); digitalWrite(pinL_DIR, HIGH); digitalWrite(pinR_DIR, HIGH); } // Case 4: Both sensors see dark -> Lost line, move forward to search if ( leftSensor && rightSensor ) { analogWrite(pinL_PWM, 150); analogWrite(pinR_PWM, 150); digitalWrite(pinL_DIR, HIGH); digitalWrite(pinR_DIR, HIGH); } } }