Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
while(1) { //store sensor data sensor_left=a2dConvert8bit(5); sensor_right=a2dConvert8bit(4); //detects more light on left side of robot if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold) {//go left servo_left(44); servo_right(44); } //detects more light on right side of robot else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold) {//go right servo_left(25); servo_right(25); } //light is about equal on both sides else {//go straight servo_left(25); servo_right(44); } /* Servo Test Code i=250; while(i>0) { servo_left(40); i--; } i=250; while(i>0) { servo_left(24); i--; } */ //rprintf("Initialization Complete\r\n"); //output message to serial (use hyperterminal) //print("Hello, World! Read My Analog: %u\r\n", sensor_0); delay_cycles(500);//a small delay to prevent crazy oscillations } /*********ADD YOUR CODE ABOVE THIS LINE **********/ return 0; }
sensor_left=a2dConvert8bit(5);sensor_right=a2dConvert8bit(4);servo_left(44);servo_right(44);servo_left(25);servo_right(25);
#include <Servo.h> //Haven't checked pin numbers...Servo Leftservo, Rightservo; // create servo objects to control a servoconst int leftpin = 0, rightpin=0; // analog pin used to connect the photodiodeint left, right; // variable to read the value from the analog pin void setup();void updateServo();int main() { setup(); while(1){updateServo;delay(15);} //waits for the servo to get there (is this needed with rotation?) return 0;} void setup() { LeftServo.attach(9); // attaches the servo on pin 9 to the servo object RightServo.attach(10);} void updateServo(){ left = analogRead(leftpin); // reads the value of the photodiode (value between 0 and 1023) right = analogRead(rightpin); left = map(left, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) right = map(right, 0, 1023, 0, 179); Leftservo.write(left); // sets the servo position according to the scaled value Rightservo.write(right);}
You can't have a main() loop, Arduion code isn't true C.setup() automatically runs once first, then the loop() one runs continuously.
#include <Servo.h> //Haven't checked pin numbers...Servo Leftservo, Rightservo; // create servo objects to control a servoconst int leftpin = 0, rightpin=0; // analog pin used to connect the photodiodeint left, right; // variable to read the value from the analog pin void setup() { LeftServo.attach(9); // attaches the servo on pin 9 to the servo object RightServo.attach(10);} void loop(){ left = analogRead(leftpin); // reads the value of the photodiode (value between 0 and 1023) right = analogRead(rightpin); left = map(left, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) right = map(right, 0, 1023, 0, 179); Leftservo.write(left); // sets the servo position according to the scaled value Rightservo.write(right); delay(15);}