Last active
February 24, 2021 12:15
-
-
Save doojinkang/46574a10fb62ef2d6d4d9d903967b3d3 to your computer and use it in GitHub Desktop.
Bluno nano project
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
int motorPin1 = 5; | |
int motorPin2 = 9; | |
void setup() | |
{ | |
pinMode(motorPin1, OUTPUT); | |
pinMode(motorPin2, OUTPUT); | |
Serial.begin(115200); | |
} | |
int speedLeft = 0; | |
int speedRight = 0; | |
void loop() { | |
if (Serial.available()) | |
{ | |
int val = Serial.read(); | |
if (val == 'L') { | |
inc(&speedLeft); | |
analogWrite(motorPin1, speedLeft); | |
} | |
else if (val == 'l') { | |
dec(&speedLeft); | |
analogWrite(motorPin1, speedLeft); | |
} | |
else if (val == 'R') { | |
inc(&speedRight); | |
analogWrite(motorPin2, speedRight); | |
} | |
else if (val == 'r') { | |
dec(&speedRight); | |
analogWrite(motorPin2, speedRight); | |
} | |
printBuf(speedLeft, speedRight); | |
} | |
} | |
void inc(int *speed) { | |
if (*speed < 250) | |
*speed += 10; | |
} | |
void dec(int *speed) { | |
if (*speed > 0 ) | |
*speed -= 10; | |
} | |
void printBuf(int s1, int s2) { | |
char buf[10]; | |
Serial.write(itoa(s1, buf, 10)); | |
Serial.write(","); | |
Serial.write(itoa(s2, buf, 10)); | |
Serial.write(" "); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment