Coding
//PROGRAM#1
int presis=A0;
int led=D0;
int led2=D2;
int led3=D7;
int val=0;
int servoPin=A5;
Servo myServo;
int servoPos=0;
int newPos=0;
String packet="abcd";
void setup()
{
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
Particle.variable("pot",&presis,INT);
Particle.variable("light",&val,INT);
Particle.variable("servopos1",&newPos,INT);
Particle.variable("servopos2",&servoPos,INT);
myServo.attach(A5);
myServo.write(0);
Particle.subscribe("blinkgreen", myHandlergreen);
}
void loop()
{
val=analogRead(presis);
val=map(val,0,4095,0,255);
newPos=map(val,0,255,0,180);
servoPos=constrain(newPos, 0, 180);
//myServo.write(servoPos);
analogWrite(led, val);
newPos=0;
packet= String(servoPos);
Particle.publish("blinkred",packet,1);
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led2, LOW);
delay(500);
}
void myHandlergreen(const char *blinkgreen, const char *data)
{
digitalWrite(led3, HIGH);
String datas=data;
//digitalWrite(led2, HIGH);
int pos=datas.toInt();
myServo.write(pos);
//digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
//PROGRAM#2
int presis=A0;
int led=D0;
int led2=D2;
int led3=D7;
int val=0;
int servoPin=A5;
Servo myServo;
int servoPos=0;
int newPos=0;
String packet="abcd";
void setup()
{
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
Particle.variable("pot",&presis,INT);
Particle.variable("light",&val,INT);
Particle.variable("servopos1",&newPos,INT);
Particle.variable("servopos2",&servoPos,INT);
myServo.attach(A5);
myServo.write(0);
Particle.subscribe("blinkred", myHandlerred);
}
void loop()
{
val=analogRead(presis);
val=map(val,0,4095,0,255);
newPos=map(val,0,255,0,180);
servoPos=constrain(newPos, 0, 180);
//myServo.write(servoPos);
analogWrite(led, val);
newPos=0;
packet= String(servoPos);
Particle.publish("blinkgreen",packet,1);
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led2, LOW);
delay(500);
}
void myHandlerred(const char *blinkred, const char *data)
{
digitalWrite(led3, HIGH);
String datas=data;
//digitalWrite(led2, HIGH);
int pos=datas.toInt();
myServo.write(pos);
//digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .