I don't know much C, but I am currently trying to make a solar tracker with an x-axis 360 degree continous servo on the bottom that follows light using two photoresistors and a y-axis 180 degree servo that can be controlled with a computer mouse.
(I found the instructions on various websites;
Mouse code: Arduino Playground - SingleServoExample
Original solar tracker code: http://thegeekbucketlist.wordpress.com/ )
I have connected the hardware and i have made both codes work seperatly. When i try to merge the codes, only the x servo works. However, when I switch places of the code within the void loop, only the y servo works (after running the code for processing ofcourse).
Can anyone help me fix this problem and help me write/write the code i need? I have tried for so many hours, and any help would be greatly appriciated.
Any help is very apriciated, also please let me know if you need more information in order to help me.
The (very untidy) code:
#include <Servo.h>
int ServoOut = 9;
int photoPinLeft = 0;
int photoPinRight = 5;
int LeftReading;
int RightReading;
int pos;
int tolerance = 100;
Servo servox; Servo servoy;
void setup() {
servoy.attach(16);
servox.attach(ServoOut);
Serial.begin(9200);
}
void loop() {
LeftReading = analogRead(photoPinLeft);
RightReading = analogRead(photoPinRight);
Serial.print("Left Reading = ");
Serial.println(LeftReading);
Serial.print("Right Reading = ");
Serial.println(RightReading);
if(LeftReading > RightReading + tolerance)
{
pos = 58;
}
else if(LeftReading + tolerance < RightReading)
{
pos = 52;
}
else
{
pos = 55;
}
servox.write(pos);
delay(120);
static int v = 0;
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case '0'...'9':
v = v * 10 + ch - '0';
break;
case 's':
servoy.write(v);
v = 0;
break;
}
}
}
And the processing code i use:
import processing.serial.*;
int gx = 15;
int gy = 35;
int spos=90;
float leftColor = 0.0;
float rightColor = 0.0;
Serial port; // The serial port
void setup()
{
size(720, 720);
colorMode(RGB, 1.0);
noStroke();
rectMode(CENTER);
frameRate(100);
println(Serial.list()); // List COM-ports
//select second com-port from the list
port = new Serial(this, Serial.list()[0], 9200);
}
void draw()
{
background(0.0);
update(mouseX);
fill(mouseX/4);
rect(150, 320, gx*2, gx*2);
fill(180 - (mouseX/4));
rect(450, 320, gy*2, gy*2);
}
void update(int x)
{
//Calculate servo postion from mouseX
spos= x/4;
//Output the servo position ( from 0 to 180)
port.write("s"+spos);
// Just some graphics
leftColor = -0.002 * x/2 + 0.06;
rightColor = 0.002 * x/2 + 0.06;
gx = x/2;
gy = 100-x/2;
}