![知识共享许可协议](https://csdnimg.cn/release/phoenix/images/creativecommons/80x15.png)
有害气体检测小车
从代码已经可以看得出电路应该怎么接了, 具体电路就不放上来了
Arduino控制部分代码
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#define RightGo 6
#define RightBack 7
#define LeftGo 5
#define LeftBack 4
#define LEFT '3'
#define RIGHT '4'
#define GO '1'
#define BACK '2'
#define STOP '0'
#define mq2 A0
#define buzzPin 3
#define ledPin 2
void setup() {
Serial.begin(9600);
pinMode(RightGo, OUTPUT);
pinMode(RightBack, OUTPUT);
pinMode(LeftGo, OUTPUT);
pinMode(LeftBack, OUTPUT);
pinMode(ledPin, OUTPUT);
initCar();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing");
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Group 3: ");
lcd.setCursor(4, 1);
lcd.print("Gas:");
lcd.print(map(analogRead(mq2), 0, 1023, 0, 100));
lcd.print("% ");
if (analogRead(mq2) > 200) {
digitalWrite(buzzPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(buzzPin, LOW);
digitalWrite(ledPin, LOW);
}
Serial.print("Gas:");
Serial.println(map(analogRead(mq2), 0, 1023, 0, 100));
Serial.print("%");
if(Serial.available() > 0) {
char ch = Serial.read();
if(ch == GO)
go();
else if(ch == BACK)
back();
else if(ch == LEFT)
turnLeft();
else if(ch == RIGHT)
turnRight();
else if(ch == STOP)
stopCar();
}
delay(500);
}
void initCar() {
digitalWrite(RightGo, LOW);
digitalWrite(RightBack, LOW);
digitalWrite(LeftGo, LOW);
digitalWrite(LeftBack, LOW);
}
void turnLeft() {
digitalWrite(RightGo, HIGH);
digitalWrite(RightBack, LOW);
digitalWrite(LeftGo, LOW);
digitalWrite(LeftBack, LOW);
}
void turnRight() {
digitalWrite(RightGo, LOW);
digitalWrite(RightBack, LOW);
digitalWrite(LeftGo, HIGH);
digitalWrite(LeftBack, LOW);
}
void go() {
digitalWrite(RightGo, HIGH);
digitalWrite(RightBack, LOW);
digitalWrite(LeftGo, HIGH);
digitalWrite(LeftBack, LOW);
}
void back() {
digitalWrite(RightGo, LOW);
digitalWrite(RightBack, HIGH);
digitalWrite(LeftGo, LOW);
digitalWrite(LeftBack, HIGH);
}
void stopCar() {
initCar();
}