Go Down

Topic: Interfacing with AutoHotkey (Easy GUI's on Windows & Linux) (Read 14 times) previous topic - next topic

Riaan Deyzel

Good day

I would like to know of the following scetch will work for the "Here is that example slightly modifed to have more buttons" to control two servos on D9 & D10?, as the code supplied does not have servo code in it.

Code: [Select]
#include <Servo.h>

Servo myservo1; 
Servo myservo2;
int command = 0 ;

void setup() {
// put your setup code here, to run once:
delay(1000);          // wait a bit before starting
Serial.begin(9600);   // set up Serial library at 9600 bps
myservo1.attach(9);
myservo2.attach(10);
}

void loop() {
  // put your main code here, to run repeatedly:
    if (Serial.available() > 0) {
    command = Serial.read();
    myservo1.write(command);                  // sets the servo position according to the scaled value
    Serial.println(command);
    delay(15);
    myservo2.write(command);                  // sets the servo position according to the scaled value
    Serial.println(command);
    delay(15);
   
    if (command == '1')
//pan left
    if (command == '2')
//pan center
    if (command == '3')
//pan right
    if (command == '4')
//tilt up
    if (command == '5')
//tilt center
    if (command == '6')
//tilt down
   
    command = 0; 
}
}

kruzan

sorry, i thought if you were far enough along to try to make a gui to move your servos, you would already know what values to use to point the servo to a position

maybe this helps?
Code: [Select]

#include <Servo.h>

Servo myservo1; 
Servo myservo2;
int command = 0 ;

void setup() {
// put your setup code here, to run once:
delay(1000);          // wait a bit before starting
Serial.begin(9600);   // set up Serial library at 9600 bps
myservo1.attach(9);
myservo2.attach(10);
}

void loop() {
  // put your main code here, to run repeatedly:
    if (Serial.available() > 0) {
    command = Serial.read();
    if (command == '1'){
//pan left
myservo1.write(0);                 
Serial.println(command);
delay(15);
    }else if (command == '2'){
//pan center
myservo1.write(90);                 
Serial.println(command);
delay(15);
    }else if (command == '3'){
//pan right
myservo1.write(180);               
Serial.println(command);
delay(15);
    }else if (command == '4'){
//tilt up
myservo2.write(0);                 
Serial.println(command);
delay(15);
    }else if (command == '5'){
//tilt center
myservo2.write(90);               
Serial.println(command);
delay(15);
    }else if (command == '6'){
//tilt down
myservo2.write(180);             
Serial.println(command);
delay(15);
    }
    command = 0; 
}
}

Riaan Deyzel

Thank you very much kruzan.

I will give it a go this weekend, and see how it works.

Regards,
Riaan Deyzel

Riaan Deyzel

Hi,

I have tested this code, and it did not work, I will look further for the same app to control 2 servo's, but will look for c# code, as I do not know AutoHotKey at all.

Regards,
Riaan Deyzel

Jaaptina

In the arduino code command is defined as int. But void loop says (command == "1"). This is not okay. Changing it to if (command == '1') is better. But is seems akh is not sending a '1'. If I change to if (command == 1) than arduino recognises the command form akh. This way sending commands to Arduino works wel. However the akh GUI is still showing chineese caracters in response. So help is needed.

My arduino code with lcd shield:

#include <LCD4Bit_mod.h>           // initialise LCD library
LCD4Bit_mod lcd = LCD4Bit_mod(2);

int command = 0 ;
char buf[12];

void setup() {
  delay(1000);          // wait a bit before starting
  Serial.begin(115200);   // set up Serial library at 9600 bps
  lcd.init();
  lcd.clear();           // clear screen of old display
  delay(1000);           // time to see the cleared screen
  lcd.printIn("Start");  // visible start after reset
  delay(1000);           // time to see the "Start" displayed
  lcd.clear();           // clear screen before display of first value
  delay(1000);
}

void loop() {
  if (Serial.available() > 0) {
    lcd.clear();
    command = Serial.read();
    if (command == 1){show();}
    if (command == '2'){show();} // faulty
    if (command == '3'){show();} // fauly
    if (command == 4){show();}
    if (command == 5){show();}
    if (command == 6){show();}
    command = 0; 
  }
}

void show(){
  Serial.println(command);
  lcd.printIn(itoa(command, buf, 10));
}

Go Up