This gist was automatically pasted at the request of the code author or one of the discord server helpers. If you have any suggestions or bugs to report, you can do so on our GitHub repository, or in our discord server. This project is run by volunteers so feel free to fork and commit your changes then open a pull request!
Created
November 23, 2020 16:41
-
-
Save ArduinoDiscordBot/9c80a02ccb95bafa34a30530f44d8411 to your computer and use it in GitHub Desktop.
Code by Moh#1234 - Mon Nov 23 2020 16:40:59 GMT+0000 (Coordinated Universal Time)
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
arduino | |
#define trimmerin A0 | |
#define H_in3 3 | |
#define H_in4 4 | |
#define motorcontrol 5 | |
#define encoder 2 | |
#define leftswitchpin 8 | |
#define rightswitchpin 12 | |
volatile int count=0; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(encoder, INPUT_PULLUP); | |
pinMode(leftswitchpin, INPUT); | |
pinMode(rightswitchpin, INPUT); | |
pinMode(motorcontrol, OUTPUT); | |
pinMode(H_in3, OUTPUT); | |
pinMode(H_in4, OUTPUT); | |
attachInterrupt(digitalPinToInterrupt(encoder), pulse, RISING); | |
} | |
void pulse() | |
{ | |
count++; | |
} | |
void loop() | |
{ | |
float voltageunmapped=analogRead(trimmerin); | |
float voltage=voltageunmapped*5/1023; | |
float percent=voltage*100/5; | |
Serial.print(voltage); | |
Serial.print("V "); | |
Serial.print(percent); | |
Serial.print("% "); | |
count=0; | |
delay(200); | |
Serial.print(count); | |
int trimmercontrol=map(percent,0, 100, 0, 255); | |
analogWrite(motorcontrol,trimmercontrol); | |
int leftswitch=digitalRead(leftswitchpin); | |
int rightswitch=digitalRead(rightswitchpin); | |
if(leftswitch==1){ | |
if(rightswitch==0){ | |
digitalWrite(H_in3,HIGH); | |
digitalWrite(H_in4,LOW); | |
Serial.print(" B"); | |
} | |
else{ | |
digitalWrite(H_in3, LOW); | |
digitalWrite(H_in4, HIGH); | |
Serial.print(" F"); | |
} | |
} | |
else{ | |
digitalWrite(H_in3,LOW); | |
digitalWrite(H_in4,LOW); | |
Serial.print("H"); | |
} | |
Serial.println(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment