Introducing
Your new presentation assistant.
Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.
Trending searches
Aduino Encoder Motor Control (Entering 'Direction!Speed@' will change the encoder reading.) For example, 'a!100@' means to move forward at a value of 100.
At Arduino, this PWM output is provided by the analogWrite function, which may contain an integer from 0 to 255. This value will change the duty of the PWM from 0 to 100%.
There are two input switches and these two switches are connected mechanically.
////////////////////////////////////////////////////////////////////
/////////////////////// EBIMU FUNCTION /////////////////////////////
#define SBUF_SIZE 64
char sbuf[SBUF_SIZE];
signed int sbuf_cnt=0;
int EBimuAsciiParser(float *item, int number_of_item)
{
int n,i;
int rbytes;
char *addr;
int result = 0;
rbytes = Serial.available();
for(n=0;n<rbytes;n++)
{
sbuf[sbuf_cnt] = Serial.read();
if(sbuf[sbuf_cnt]==0x0a)
{
addr = strtok(sbuf,",");
for(i=0;i<number_of_item;i++)
{
item[i] = atof(addr);
addr = strtok(NULL,",");
}
result = 1;
// Serial.print("\n\r");
// for(i=0;i<number_of_item;i++) { Serial.print(item[i]); Serial.print(" "); }
}
else if(sbuf[sbuf_cnt]=='*')
{ sbuf_cnt=-1;
}
sbuf_cnt++;
if(sbuf_cnt>=SBUF_SIZE) sbuf_cnt=0;
}
return result;
}
/////////////////////// EBIMU FUNCTION /////////////////////////////
////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
float euler[3];
if(EBimuAsciiParser(euler, 3))
{
Serial.print("\n\r");
Serial.print(euler[0]); Serial.print(" ");
Serial.print(euler[1]); Serial.print(" ");
Serial.print(euler[2]); Serial.print(" ");
}
if((euler[0]<5)&&(euler[0]>-5)&&(euler[1]<5)&&(euler[1]>-5)) digitalWrite(LED_BUILTIN, HIGH);
else digitalWrite(LED_BUILTIN, LOW);
}
/*item:수신된 데이터를 저장할 주소를 입력합니다.
number_of_item: EBIMU의 1PACEKT 당 수신 항목 수를 입력합니다.
예) euler축 output 3
euler, acceleration output 6
#include <Encoder.h>
#include <SoftwareSerial.h>
Encoder myEnc(2, 3);
int M1 = 5; //#1 Motor PWM
int E1 = 4; //#1 Motor direction
char user_input;
int pwm_input;
String U_I;
String P_I;
void setup()
{
Serial.setTimeout(100); //readString 사용시 Delay Time default 는 1000임으로 빠르게 처리
pinMode(M1, OUTPUT); // 5번 핀을 출력핀으로 설정
pinMode(E1, OUTPUT); // 4번 핀을 출력핀으로 설정
Serial.begin(9600);
Serial.println("Number for Motor Control "); // Initial words
Serial.println("a. Forward");
Serial.println("b. Backward");
Serial.println("s. Stop");
Serial.println();
}
long oldPosition = -999;
void loop()
{
char user_input;
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
while(Serial.available()) //Serial.available은 시리얼 통신할 때 데이터 수신에 필요.
{
//user_input = Serial.read(); // Get Input from Serial Monitor
String U_I = Serial.readStringUntil('!');
String P_I = Serial.readStringUntil('@');
char user_input = U_I.charAt(0); //charAt는 index로 주어진 값에 해당 문자를 리턴한다. 왼쪽에서 0부터 시작.
int pwm_input = P_I.toInt(); //toInt는 타당한 스트링을 정수로 변환한다.
Serial.print(user_input);
Serial.print(',');
Serial.println(pwm_input);
if (user_input=='a'&& pwm_input> 0){
digitalWrite(E1, HIGH); //디지털은 0과 1밖에 없다. 그래서 HIGH와 LOW로 방향제어를 한다.
analogWrite(M1,pwm_input); //아날로그 함수의 인자로는 0~255까지 정수가 들어갈 수있다. 펄스 폭을 변조하는 데 사용된다. 속도 제어가능.
Serial.print("Motor 1 Forward");
Serial.println();
}
else if(user_input =='b' && pwm_input> 0){
digitalWrite(E1, LOW);
analogWrite(M1,pwm_input);
Serial.print("Motor 1 Backward");
Serial.println();
}
else if(user_input =='s'){
digitalWrite(E1, LOW);
analogWrite(M1,0);
Serial.print("Motor 1 Stop");
Serial.println();
}
else{
Serial.println("Wrong Number");
}
}
}