Tuesday, February 1, 2011

Profiling Machine: Second Attempt

This is the second attempt at controlling an arduino with a physical analog input.
After searching for some code to hack on the interweb I found this one by Professor Nashid Nabian in an Interactive Architecture course slide.  It controls a standard servo (180 degrees) with a potentiometer (also 180 degrees).  I find the 1:1 relationship between control and output very satisfying.

The code was adapted to show 'right' and 'wrong' at discreet angles.

The next step is to build a physical setup around it (images on the dial) that correspond to the type of moral machine we're interested in.

Code:

 /*A Moral technology by Breadboards are us
this code is a modified version of
Nabian's Responsive System Input Potentiometer- Output Standard Servo-
After a failed attempt to use push buttons to send exact angles to the
servo I found this code in an old Interactive Architecture course archive,
I really enjoy the direct relationship of turning a potentiometer and turning
a servo, moreover this code by N.Nabian has the analogRead (reading from
potentiometer) and servo control in two separate pieces of code, making it
very easy to do something extra with the reading from the potentiometer. 
AL */

//connect the potentiometer as follow:
//left from ground to left pin, Center to analog input pin number 0 , right to V5
//connect the motor as follows:
//black to ground, Red to V5, yellow to digital output pin number 13
//potentiometer range is 0-1024

void setup(){
  Serial.begin(9600);
  pinMode(13,OUTPUT);  //sets servo output on pin 13
  pinMode(5,OUTPUT);  //sets a green led output to pin 5
  pinMode(6,OUTPUT);  //sets a red led output to pin 6
  pinMode(7,OUTPUT);  //sets a green led output to pin 7
  pinMode(8,OUTPUT);  //sets a red led output to pin 8
  pinMode(9,OUTPUT);  //sets a green led output to pin 9 
}
void loop(){
  int val = analogRead(0); //this part of the code reads the signal from pot
  servoPulse(13,val);
  Serial.println(val);
 
  if (val > 160 && val < 180) // if the pulse is at about 30 degrees
{digitalWrite(5,HIGH);} //turn a green led on
 else  // if it is not
 {digitalWrite(5,LOW);} //turn a green led off

  if (val > 335 && val < 347) // if the pulse is at about 60 degrees
{digitalWrite(6,HIGH);} //turn a red led on
 else  // if it is not
 {digitalWrite(6,LOW);} //turn a red led off

  if (val > 504 && val < 518) // if the pulse is at about 90 degrees
{digitalWrite(7,HIGH);} //turn a green led on
 else  // if it is not
 {digitalWrite(7,LOW);} //turn a green led off

  if (val > 675 && val < 690) // if the pulse is at about 120 degrees
{digitalWrite(8,HIGH);} //turn a red led on
 else  // if it is not
 {digitalWrite(8,LOW);} //turn a red led off

   if (val > 845 && val < 860) // if the pulse is at about 90 degrees
{digitalWrite(9,HIGH);} //turn a green led on
 else  // if it is not
 {digitalWrite(9,LOW);} //turn a green led off

 }


void servoPulse(int pin, int angle){  // this part of the code by N.Nabian
  int pulseWidth = (angle*2)+500;  //controls the servo position
  digitalWrite(13,HIGH);  //based on the pot
  delayMicroseconds(pulseWidth);
  digitalWrite(13,LOW);
  delay(20);
}


Images:

 

No comments:

Post a Comment