Created
February 28, 2016 09:02
-
-
Save littilewing/d3eff5b4ce79699b51d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <NewPing.h> | |
#define TRIGGER_PIN 11 // Arduino pin tied to trigger pin on the ultrasonic sensor. | |
#define ECHO_PIN 15 // Arduino pin tied to echo pin on the ultrasonic sensor. | |
#define MAX_DISTANCE 108 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. | |
#define ResistanceA 1 | |
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. | |
bool isMove; | |
int motorPower; | |
bool reverse; | |
const int sensorChannels = 8; | |
const int maxNumReadings = 10; | |
int resistanceAValue = 0; | |
int smoothingValues[sensorChannels][maxNumReadings]; | |
int smoothingIndex[sensorChannels]; | |
int smoothingTotal[sensorChannels]; | |
// motor port mapping | |
#define PWM_PIN_A 9 | |
#define THISWAY_PIN_A 7 | |
#define THATWAY_PIN_A 8 | |
#define PWM_PIN_B 10 | |
#define THISWAY_PIN_B 5 | |
#define THATWAY_PIN_B 6 | |
void setup() { | |
Serial.begin(9600); | |
isMove = true; | |
reverse = false; | |
motorPower = 100; | |
// モーター用のピン設定 | |
pinMode(9, OUTPUT); //M1 analogOut | |
pinMode(7, OUTPUT); //M1 DigitalOut Thisway | |
pinMode(8, OUTPUT); //M1 DigitalOut Thatway | |
pinMode(10, OUTPUT); //M2 analogOut | |
pinMode(5, OUTPUT); //M2 DigitalOut Thisway | |
pinMode(6, OUTPUT); //M2 DigitalOut Thatway | |
} | |
void loop() { | |
int sliderValue = analogRead(A6); | |
int buttonValue = analogRead(A7); | |
int volume = analogRead(A0); | |
int sensorValue = readSonar() ;//sonar.ping() / 5 ; | |
Serial.println(sensorValue); | |
motorPower = sliderValue / 4; | |
//Serial.println(buttonValue); | |
if (buttonValue < 10 ) { | |
isMove = (isMove ? false : true); | |
} | |
if (volume > 30) { | |
reverse = (reverse ? false : true); | |
} | |
if (isMove) { | |
if (sensorValue < 50) { | |
stopHere(); | |
delay(200); | |
goBack(); | |
delay(300); | |
turnRight(); | |
delay(100); | |
} | |
else if (sensorValue < 150) { | |
if (reverse) | |
turnLeft(); | |
else | |
turnRight(); | |
// delay(50); | |
} | |
else { | |
goFoward(); | |
} | |
/* | |
if (reverse) { | |
turnLeft(); | |
} | |
else | |
{ | |
turnLeft(); | |
} | |
*/ | |
//モーター1(M1)を出力100で正回転 | |
analogWrite(9, motorPower); | |
//モーター2(M2)を出力255(最大)で逆回転 | |
analogWrite(10, motorPower); | |
} | |
else { | |
digitalWrite(8, LOW); | |
digitalWrite(6, LOW); | |
} | |
delay(1); | |
} | |
void turnRight() { | |
digitalWrite(7, LOW); | |
digitalWrite(8, HIGH); | |
digitalWrite(5, LOW); | |
digitalWrite(6, HIGH); | |
} | |
void turnLeft() { | |
digitalWrite(7, HIGH); | |
digitalWrite(8, LOW); | |
digitalWrite(5, HIGH); | |
digitalWrite(6, LOW); | |
} | |
void goFoward() { | |
digitalWrite(7, LOW); | |
digitalWrite(8, HIGH); | |
digitalWrite(5, HIGH); | |
digitalWrite(6, LOW); | |
} | |
void goBack() { | |
digitalWrite(7, HIGH); | |
digitalWrite(8, LOW); | |
digitalWrite(5, LOW); | |
digitalWrite(6, HIGH); | |
} | |
void stopHere() { | |
digitalWrite(7, LOW); | |
digitalWrite(8, LOW); | |
digitalWrite(5, LOW); | |
digitalWrite(6, LOW); | |
} | |
int readSonar() { | |
int value; | |
delay(5); // Wait 15ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. | |
value = sonar.ping() / 5 ; // Send ping, get ping time in microseconds (uS). | |
value = smoothingValue(ResistanceA, value, 10); | |
if (value == 0) value = 1023; | |
return value; | |
} | |
int smoothingValue(int channel, int value, int numReadings) { | |
int total; | |
int index = smoothingIndex[channel]; | |
total = smoothingTotal[channel] - smoothingValues[channel][index]; | |
smoothingValues[channel][index] = value; | |
smoothingTotal[channel] = total + value; | |
smoothingIndex[channel]++; | |
if (smoothingIndex[channel] >= numReadings) { | |
smoothingIndex[channel] = 0; | |
} | |
return int(round(smoothingTotal[channel] / (numReadings))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment