简体   繁体   中英

interfacing c++ to control motor stepper with arduino

I have tried to control motor stepper via arduino uno board with slider in visual C++.

but the servo didnot move at all.

here the program at PC side:

void CENVSConfigDlg::OnBnClickedButton1() 
{ 
 SetTimer(cTimer1,80,NULL); 
  }

 void CENVSConfigDlg::OnTimer(UINT_PTR ID)
 { 
   if(ID==cTimer1){ 
    DWORD nbytes;
    char buf[5]; 
    sprintf(buf, "%d \n", val_test);
     /* Open serial port. */ 
       if(!WriteFile( hnd_serial, (void*)buf, 5, &nbytes, NULL )){MessageBox(L"Write Com Port fail!");return;} 
 }

and the program in arduino:

 #include <Servo.h> 
 Servo servoMain;

int index=0; 
String inputString;

void setup() 
{ 
Serial.begin(9600);
servoMain.attach(9);
}

void loop() 
{ 
  int data;
while (Serial.available()) 
{ 
char inChar = (char)Serial.read(); 
if (inChar == '\n' || inChar == 'z') 
{
  data=stringToInt(inputString); 
Serial.println(data); //
inputString=""; 

servoMain.write(data); //tambahannya
delay(50);

break; 
} 
if (inChar != 0)
{ 
inputString += inChar;
} 
} 
}

int stringToInt(String s)
{ 
char char_string[s.length()+1]; 
 s.toCharArray(char_string, s.length()+1); 
 return atoi(char_string);
 } 

the pc i think is sending the data, but why the motor didnot working? any idea?

First off, does the serial link work at all? The number of ways that an RS232 link can not work, for both hardware and software reasons, is legendary. Unless you can show that the hardware can transfer data, it's pointless to look at your dedicated software. If you have a scope, use that to see if anything is being transmitted and then check that it arrives at the correct pin on the arduino input. If you have no access to a scope, a small LED and a 4.7K resistor can be used to indicate that there is data on the line - it will flicker with the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM