[vc_row][vc_column][vc_column_text]
[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Part list” tab_id=”1409220425-1-6″][vc_column_text]Part list
1 x pcDuino3
[/vc_column_text][/vc_tab][vc_tab title=”Wiring diagram” tab_id=”1409220425-2-11″][vc_column_text]Wiring diagram
1. First install the PWM module to the pcDuino
2.Then connect the machine Arm to the PWM module
3. Power 12V for the PWM
[/vc_column_text][/vc_tab][vc_tab title=”Test code” tab_id=”1409220454831-2-1″][vc_column_text]
Test Code
#include <SPI.h> #include<string.h> #include <stdio.h> #define STATUS_PIN 9 #define CS_PIN 10 #define MOSI_PIN 11 #define MISO_PIN 12 #define SCK_PIN 13 #define CMD_SET_ON_OFF 0x01 #define CMD_SET_POLARITY 0x02 #define CMD_SET_FREQ 0x03 #define CMD_SET_DUTY 0x04 #define USE_HARDWARE_SPI 1 #define SPI_MODE SPI_MODE0 #define MIN_DUTY 250 #define MAX_DUTY 1250 unsigned short duty[27]; unsigned char gIndex; unsigned char group; #if(!USE_HARDWARE_SPI) unsigned char SoftSpiTransfer(unsigned char data) { unsigned char cnt; unsigned char rst; rst = 0; for(cnt = 0;cnt < 8 ;cnt ++) { rst <<= 1; if(data & 0x80)digitalWrite(MOSI_PIN,HIGH); else digitalWrite(MOSI_PIN,LOW); digitalWrite(SCK_PIN,HIGH); digitalWrite(SCK_PIN,LOW); rst |= digitalRead(MISO_PIN); data <<= 1; } return rst; } #define SpiExchangeByte(x) SoftSpiTransfer(x) #elif defined PCDUINO_IDE #define SpiExchangeByte(x) SPI.transfer(x,SPI_CONTINUE) #else #define SpiExchangeByte(x) SPI.transfer(x) #endif void DeviceSynchronize(void) { SpiExchangeByte(0); SpiExchangeByte(0); SpiExchangeByte(0); SpiExchangeByte(0); SpiExchangeByte(0); SpiExchangeByte(0); SpiExchangeByte(0); SpiExchangeByte(0); } void SendCmd(unsigned char cmd,unsigned char index,unsigned short value) { unsigned char CheckSum; unsigned char valueL; unsigned char valueH; valueL = value & 0xff; valueH = ((value>>8) & 0xff); CheckSum = 0x87; SpiExchangeByte(0x87); SpiExchangeByte(cmd); CheckSum += cmd; SpiExchangeByte(index);//index CheckSum += index; SpiExchangeByte(valueL); CheckSum += valueL; SpiExchangeByte(valueH); CheckSum += valueH; SpiExchangeByte(CheckSum);//CheckSum } void SetPwmOnOff(unsigned char index,boolean on) { SendCmd(CMD_SET_ON_OFF,index,on); } void SetPwmDuty(unsigned char index,unsigned short duty) { SendCmd(CMD_SET_DUTY,index,duty); } void SetPwmFrequency(unsigned char index,unsigned short frequency) { SendCmd(CMD_SET_FREQ,index,frequency); } void setup() { // put your setup code here, to run once: #if(USE_HARDWARE_SPI) SPI.begin(); SPI.setBitOrder(MSBFIRST); SPI.setDataMode(SPI_MODE); SPI.setClockDivider(SPI_CLOCK_DIV2); #endif group = 0; gIndex = 0; duty[0]=970; duty[1]=860; duty[2]=480; duty[3]=470; duty[4]=690; duty[5]=730; duty[6]=MIN_DUTY; duty[7]=MIN_DUTY; duty[8]=MIN_DUTY; duty[9]=MIN_DUTY; duty[10]=MIN_DUTY; duty[11]=MIN_DUTY; duty[12]=MIN_DUTY; duty[13]=MIN_DUTY; duty[14]=MIN_DUTY; duty[15]=MIN_DUTY; duty[16]=MIN_DUTY; duty[17]=MIN_DUTY; duty[18]=MIN_DUTY; duty[19]=MIN_DUTY; duty[20]=MIN_DUTY; duty[21]=MIN_DUTY; duty[22]=MIN_DUTY; duty[23]=MIN_DUTY; duty[24]=MIN_DUTY; duty[25]=MIN_DUTY; duty[26]=MIN_DUTY; DeviceSynchronize(); SetPwmDuty(0,970); SetPwmDuty(1,860); SetPwmDuty(2,480); SetPwmDuty(3,470); SetPwmDuty(4,690); SetPwmDuty(5,730); } void loop() { // put your main code here, to run repeatedly: char tmpChar; scanf("%c",&tmpChar); if((tmpChar <'8')&&(tmpChar >= '0')) { gIndex = tmpChar - '0'; SetPwmOnOff(gIndex+group,1); } else if (tmpChar == 'z') { group = 0; } else if (tmpChar == 'x') { group = 8; } else if (tmpChar == 'c') { group = 16; } else if (tmpChar == 'v') { group = 24; } else if (tmpChar == 'w') { if(duty[4] < MAX_DUTY)duty[4] += 10; SetPwmDuty(4,duty[4]); } else if (tmpChar == 's') { if(duty[4] > MIN_DUTY)duty[4] -= 10; SetPwmDuty(4,duty[4]); } else if (tmpChar == 'a') { if(duty[5] < MAX_DUTY)duty[5] += 10; SetPwmDuty(5,duty[5]); } else if (tmpChar == 'd') { if(duty[5] > MIN_DUTY)duty[5] -= 10; SetPwmDuty(5,duty[5]); } else if (tmpChar == 'i') { if(duty[3] <MAX_DUTY)duty[3] += 10; SetPwmDuty(3,duty[3]); } else if (tmpChar == 'k') { if(duty[3] > MIN_DUTY) duty[3] -= 10; SetPwmDuty(3,duty[3]); } else if (tmpChar == 'o') { if(duty[2] < MAX_DUTY)duty[2] += 10; SetPwmDuty(2,duty[2]); } else if (tmpChar == 'l') { if(duty[2] > MIN_DUTY)duty[2] -= 10; SetPwmDuty(2,duty[2]); } else if (tmpChar == 'f') { if(duty[1] < MAX_DUTY)duty[1] += 10; SetPwmDuty(1,duty[1]); } else if (tmpChar == 'h') { if(duty[1] > MIN_DUTY)duty[1] -= 10; SetPwmDuty(1,duty[1]); } else if (tmpChar == 't') { if(duty[0] < MAX_DUTY)duty[0] += 10; SetPwmDuty(0,duty[0]); } else if (tmpChar == 'g') { if(duty[0] > MIN_DUTY) duty[0] -= 10; SetPwmDuty(0,duty[0]); } if(tmpChar >= '0')printf("D0:%d-D1:%d-D2:%d-D3:%d-D4:%d-D5:%d-n",duty[0],duty[1],duty[2],duty[3],duty[4],duty[5]); }
[/vc_column_text][/vc_tab][vc_tab title=”Result show” tab_id=”1409220455581-3-9″][vc_column_text]After you input the code to the Arduino IDE, click unload.
Result show
An interface will show, and you can click the command “g” to enlarge the machine Arm, or click “t” to smaller the machine Arm as the video show
Have a nice day, thank you for you watching, hope you can have a try to it !
Leave a Reply
You must be logged in to post a comment.