Arduino

From SUNY Potsdam ACM
Jump to: navigation, search

<--- Back

This section is reserved for Arduino code and information. Do not forget to initial your postings. Use the syntax highlighter.


Contents

Template for Posting

Go to the bottom of the postings, and paste this snippet and edit to your liking.


== My Posting ==
Description

<syntaxhighlight lang="c">

   //Your code here

</syntaxhighlight>


Controlling Two Servos

This is some simple code for the Motor Shield for the Arduino. This was taken from the facebook group and was written by Sergey Zolotykh.

/*
Arduino serial - Two motor controller
Hardware:
  - Motor controller shield
  - Two servo motors
  - Serial input format:
Serial input format:
    Protocol: motor_number=angle
    Example: 1=180@ or 2=56@
*/
#include <ServoTimer1.h>
 
ServoTimer1 servo1;
ServoTimer1 servo2;
int inByte = 0;
char buf[256];
char stopByte='@';
 
void setup(){
  delay(500);
  Serial.begin(9600);
  servo1.attach(10);
  servo2.attach(9);
  Serial.println("I am ready");
}
void loop(){
   int i=0;
   while(true){
     if (Serial.available() > 0) {
       inByte = Serial.read();
       if(inByte==stopByte){
         buf[i]='\0';
         break;
       }
       buf[i]=inByte;
       i++;
      Serial.print("I received: ");
      Serial.println(inByte);
      }
    }
    Serial.print("I received string: ");
    Serial.println(buf);
    char motor = buf[0];
 
    int k = 2;
    while(buf[k] != '\0'){
        buf[k-2] = buf[k];
        k++;
    }
    buf[k-2] = '\0';
    int angle=atoi(buf);
 
    if(motor == '1'){
      Serial.print("Motor is 1, and angle is: ");
      Serial.println(angle);
      servo1.write(angle);
      }else if(motor == '2'){
        Serial.print("Motor is 2, and angle is: ");
        Serial.println(angle);
        servo2.write(angle);
    }
}


Basic Serial Communication with Python

This will get you quick and dirty access to the arduino using python and the pyserial library. Notice that this works with python2.

--ac

#!/usr/bin/env python2
 
import serial
 
# Change this for your device
arduino = serial.Serial("/dev/ttyACM0", 9600)
 
def arduino_read(length=10):
  i = length
  while(i > 0):
    arduino.readline()
    i = i - 1
 
def arduino_write(buff):
  arduino.write(buff)
 
 
arduino_write("Hello")
arduino_read(5)

Template for Posting

Go to the bottom of the postings, and paste this snippet and edit to your liking.


== My Posting ==
Description

<syntaxhighlight lang="c">

   //Your code here

</syntaxhighlight>


Controlling Two Servos

This is some simple code for the Motor Shield for the Arduino. This was taken from the facebook group and was written by Sergey Zolotykh.

/*
Arduino serial - Two motor controller
Hardware:
  - Motor controller shield
  - Two servo motors
  - Serial input format:
Serial input format:
    Protocol: motor_number=angle
    Example: 1=180@ or 2=56@
*/
#include <ServoTimer1.h>
 
ServoTimer1 servo1;
ServoTimer1 servo2;
int inByte = 0;
char buf[256];
char stopByte='@';
 
void setup(){
  delay(500);
  Serial.begin(9600);
  servo1.attach(10);
  servo2.attach(9);
  Serial.println("I am ready");
}
void loop(){
   int i=0;
   while(true){
     if (Serial.available() > 0) {
       inByte = Serial.read();
       if(inByte==stopByte){
         buf[i]='\0';
         break;
       }
       buf[i]=inByte;
       i++;
      Serial.print("I received: ");
      Serial.println(inByte);
      }
    }
    Serial.print("I received string: ");
    Serial.println(buf);
    char motor = buf[0];
 
    int k = 2;
    while(buf[k] != '\0'){
        buf[k-2] = buf[k];
        k++;
    }
    buf[k-2] = '\0';
    int angle=atoi(buf);
 
    if(motor == '1'){
      Serial.print("Motor is 1, and angle is: ");
      Serial.println(angle);
      servo1.write(angle);
      }else if(motor == '2'){
        Serial.print("Motor is 2, and angle is: ");
        Serial.println(angle);
        servo2.write(angle);
    }
}


Basic Serial Communication with Python

This will get you quick and dirty access to the arduino using python and the pyserial library. Notice that this works with python2.

--ac

#!/usr/bin/env python2
 
import serial
 
# Change this for your device
arduino = serial.Serial("/dev/ttyACM0", 9600)
 
def arduino_read(length=10):
  i = length
  while(i > 0):
    arduino.readline()
    i = i - 1
 
def arduino_write(buff):
  arduino.write(buff)
 
 
arduino_write("Hello")
arduino_read(5)
Personal tools