Go Down

Topic: lordvon64 youtube demos code (Read 567 times) previous topic - next topic

lordvon


lordvon

Hello,

I usually have my code and stuff on my own website, but its been down so I will just put code here. Because this is my first post here, I am not allowed to post w/ links, but you can see the demos under lordvon64's channel.
Demos:
Arduino Xbee Wireless Full-Range Joystick FPS Servo Turret:
Code: [Select]
//CONTROLLER CODE
//Feel free to use this code however you like, but please credit lordvon and this site!
//'ud' and 'lr' are the pot signals for the up/down and left/right pots, respectively.

//(Joysticks and their pot values are not perfect, so some things have to be empirically
//determined. The serial read function is a great tool for collecting the values.)
//range of ud: 29-1014 (middle: around 479-540; avg:~510, center:+/-35)
//range of lr: 0-960 (middle: around 419-471; avg:445, center:+/-30)
//minimum range extreme (from avg) magnitude: 445
//maximum tolerance value: 35

//Print values as 'topic:value', where 'topic' denotes either 'ud' or 'lr',
//and 'value' is the associated pot value.
//Reciever is prompted to record by ':', and ended by ','.

//"" gives characters, '' gives characters in integer form.

//Empirically determined and/or arbitrary constants.
#define ud1 0
#define lr1 1
#define highpulse 2400
#define lowpulse 600
#define lr1avg 445
#define ud1avg 510
#define tolerance1 40
#define range1 445
#define maxadd 35

int ud1val;
int lr1val;
int lr1pulse;
int ud1pulse;
int lr1previous;
int ud1previous;
boolean update;

void setup()
{
 Serial.begin(9600);
 //initialize servo to its center position.
 lr1previous = lr1avg;
 ud1previous = ud1avg;
 Serial.print(':');
 Serial.print((highpulse-lowpulse)/2+lowpulse);
 Serial.print(',');
 Serial.print((highpulse-lowpulse)/2+lowpulse);
}

void loop()
{
 ud1val = analogRead(ud1);
 lr1val = analogRead(lr1);
 update = (abs(lr1avg-lr1val) > tolerance1) || (abs(ud1avg-ud1val) > tolerance1);  
 if (update)
 {
   lr1val = newval(lr1val,(lr1avg-range1),(lr1avg+range1),lr1avg,lr1previous);
   ud1val = newval(ud1val,(ud1avg-range1),(ud1avg+range1),ud1avg,ud1previous);
   lr1previous = lr1val;
   ud1previous = ud1val;
   lr1pulse = map(lr1val,(lr1avg-range1),(lr1avg+range1),lowpulse,highpulse);
   ud1pulse = map(ud1val,(ud1avg-range1),(ud1avg+range1),lowpulse,highpulse);    
   //':' is packet delimiter, ',' is UD/LR delimiter.
   //Each cycle of delimiters denote a packet.
   //Packet: UD value first, then LR value. (:UD,LR:)
   Serial.print(':');
   Serial.print(ud1pulse);
   Serial.print(',');
   Serial.print(lr1pulse);
   delay(25);    
 update = false;
 }
}

int newval(int val, int minval, int maxval, int avgval, int previousval)
{
 int increment;
 increment = val-avgval;
 if (increment > 0)
 {
   increment = increment - tolerance1;
 }
 else if (increment < 0)
 {
   increment = increment + tolerance1;
 }
 increment = map(increment,0,range1-tolerance1,0,maxadd);
 val = increment + previousval;
 if (!(val <= maxval) || !(val >= minval))
 {
   if (val > maxval)
   {
     val = maxval;
   }
   else
   {
     val = minval;
   }
 }
 return val;
}

Code: [Select]
//RECIEVER CODE
//Feel free to use this code however you like, but please credit lordvon and this site!
#include <Servo.h>

Servo elevation;
Servo azimuth;

#define highpulse 2400
#define lowpulse 600

char received;
char pulsechar[4];
int counter=0;
int pulse;
boolean delimiter;

void setup()
{
 Serial.begin(9600);
 while (!Serial.available()){}
 received = Serial.read();
 elevation.attach(12);
 azimuth.attach(13);
}

void loop()
{
//  if (Serial.available())
//  {
//    Serial.println(char(Serial.read()));
//  }
 delimiter = (received == ',') || (received == ':');
 if (!delimiter)
 {
   counter = 0;
   while (!delimiter)
   {
     while (!Serial.available()){}
     pulsechar[counter] = received;
     counter += 1;
     received = Serial.read();
     delimiter = (received == ',') || (received == ':');
   }
   pulsechar[counter] = '\0';
   pulse = atoi(pulsechar);
   switch (received)
   {
     case ',':
       azimuth.writeMicroseconds(pulse);
       break;
     case ':':
       elevation.writeMicroseconds(pulse);
       break;
   }
 }
 else
 {
   while (!Serial.available()){}
   received = Serial.read();
 }
}



Wireless PWM with ADC: Xbee with No Microcontroller (Direct I/O)
(just configuration code for the a hyperterminal (i used xctu))
Code: [Select]
//pot (potentiometer) reader configuration
//initialization: type "+++", WAIT few seconds for ok,
//then type "AT" and press enter, get ok, then issue commands.
//(numbers are in hexadecimal format)

ATID 4064
ATMY 40
ATDL 64
ATD1 2
ATIR A //(A=10 in hexadecimal format)
ATIT 1
ATWR

Code: [Select]
//pwm outputter
//initialization: type "+++", WAIT few seconds for ok,
//then type "AT" and press enter, get ok, then issue commands.
//(numbers are in hexadecimal format)

ATID 4064
ATMY 64
ATDL 40
ATP1 2
ATIU 1
ATIA 40
ATWR



Arduino Servo Turret Controlled by a Nintendo DS Touch Screen
Code: [Select]

//Code was adapted from that found on (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1243499684/0).
#include <Servo.h>

Servo azimuth;
Servo elevation;

// Empirically determined; Voltage between an output high and output low pin.
#define Vref 4.46
#define highpulse 2400
#define lowpulse 600
#define quarter (highpulse-lowpulse)/4
#define s1high lowpulse+quarter/2
#define s2high s1high+quarter
#define s3high s2high+quarter
#define s4high s3high+quarter
#define sdivide .7071
#define maxmag 420

//When looking at DS screen with connector bottom left,
//      the connections are from left to right:
//TOP, X1
//LEFT, Y2
//BOTTOM, X2
//RIGHT, Y1

// Digital pin connections (used to drive power)
#define X1 2 // TOP to Digital output 5
#define Y2 3 // LEFT to digital output 2
#define X2 4 // BOTTOM to digital output 3
#define Y1 5 // RIGHT to digital output 4

// Analog inputs
#define Xin 3 // From X1
#define Yin 4 // From Y1

// set initial touched position
int touchX;
int touchY;
int xpos;
int ypos;
float cosine;
float sine;
float mag;
int apulse;
int epulse;

void setup()
 {
 azimuth.attach(11);
 elevation.attach(12);
 }

void loop()
 {
 if (touched())
   {
   xpos = touchX - 511;
   ypos = touchY - 511;
   if(!(xpos == 0 and ypos == 0))
     {
     mag = sqrt(pow(ypos,2) + pow(xpos,2));
     sine = ypos / mag;
     if(abs(sine)>=sdivide)
       {
       cosine = xpos / mag;
       if(ypos>0)
         {
         apulse = map(cosine*10000,-7071,7071,s2high,s3high);
         }
       else
         {
         if(xpos >= 0)
           {
           apulse = map(cosine*10000,7071,0,s4high,highpulse);
           }
         else
           {
           apulse = map(cosine*10000,0,-7071,lowpulse,s1high);
           }
         }
       }
     else
       {
       if(xpos>0)
         {
         apulse = map(sine*10000,7071,-7071,s3high,s4high);
         }
       else
         {
         apulse = map(sine*10000,-7071,7071,s1high,s2high);
         }
       }
     }
   azimuth.writeMicroseconds(apulse);
   if(mag>maxmag or mag<0)
     {
     mag=maxmag/2;
     }
   epulse = map(mag,0,maxmag,highpulse,lowpulse);
   elevation.writeMicroseconds(epulse);
   }
 }

boolean touched()
 {
 boolean touch = false;
 // Horizontal routine Y1:Y2::Vcc:Gnd
 pinMode(Y2, OUTPUT);
 digitalWrite(Y2, LOW);
 pinMode(Y1, OUTPUT);
 digitalWrite(Y1, HIGH);
 // X1,X2 high impedance (input mode)
 pinMode(X1, INPUT);
 pinMode(X2, INPUT);
 // wait a bit, then read from X1
 delay(10);
 touchX = analogRead(Xin);
 // Vertical routine X1:X2::Vcc:Gnd
 pinMode(X2, OUTPUT);
 digitalWrite(X2, LOW);
 pinMode(X1, OUTPUT);
 digitalWrite(X1, HIGH);
 // Y1,Y2 high impedance (input mode)
 pinMode(Y1, INPUT);
 pinMode(Y2, INPUT);
 // wait a bit, then read from Y1
 delay(10);
 touchY = analogRead(Yin);

 // Only read touch if coords are below 1000 and above 0
 //      (stops errors with certain screens)
 if(touchX < 1023 and touchX > 0 and touchY < 1023 and touchY > 0)
   {
   touch = true;
   }

 return touch;
 }

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy