Ciao, questo è il completo, nulla di particolare. in effetti la funzione mi deve far funzionare un servo su due condizioni. 1 se al sensore luce arriva poca luce, 2 se viene premuto un tasto dal tablet.
int lucePin = 11;
int farettiPin = 12;
String readString;
#include <Servo.h>
Servo myservo;
//----------------------------
const int analogInPin = A5;
const int analogOutPin = 1;
int sensorValue = 0; //int sensorValue =0;
int outputValue = 0;
int serial_val;
int VERDE = 3;
int BLU = 5;
int ROSSO = 6;
//----------------------------
void setup() {
Serial.begin(9600);
Serial.setTimeout(4);
pinMode(lucePin, OUTPUT);
pinMode(farettiPin, OUTPUT);
myservo.attach (9);
//-------------------------------------------------
pinMode(VERDE, OUTPUT);
pinMode(BLU, OUTPUT);
pinMode(ROSSO, OUTPUT);
//-------------------------------------------------
}
void loop()
{
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print(" " );
Serial.println(sensorValue);
readString="";
/*
if (sensorValue > 50 && sensorValue < 90) {
}
else if (sensorValue > 90 && sensorValue < 130) {
myservo.write (135);}
else if (sensorValue > 130 && sensorValue < 190) {
myservo.write (90);}
else if (sensorValue > 190 && sensorValue < 250) {
myservo.write (45);}
else if (sensorValue > 250 && sensorValue < 680) {
myservo.write (0);}*/
delay(1000);
while (Serial.available())
{
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0)
{
Serial.println(readString);
if (readString == "luceon")
{
digitalWrite(lucePin, HIGH);
}
if (readString == "farettion")
{
digitalWrite(farettiPin, HIGH);
}
if (readString == "luceoff")
{
digitalWrite(lucePin, LOW);
}
if (readString == "farettioff")
{
digitalWrite(farettiPin, LOW);
}
if (sensorValue > 50 && sensorValue < 90) {
myservo.write (180); }
else if (sensorValue > 90 && sensorValue < 130) {
myservo.write (135);}
else if (sensorValue > 130 && sensorValue < 190) {
myservo.write (90);}
else if (sensorValue > 190 && sensorValue < 250) {
myservo.write (45);}
// else if (sensorValue > 250 && sensorValue < 680) {
// myservo.write (0);}
if (readString == "zero" ) {
int zero = Serial.read ();
Serial.println (zero);
myservo.write (0);
}
else if (sensorValue > 250 || sensorValue < 800) {
int zero = Serial.read ();
Serial.println (zero);
myservo.write (0);}
if (readString == "quattro")
{
int quattro = Serial.read ();
Serial.println (quattro);
myservo.write (45);
}
if (readString == "nove")
{
int nove = Serial.read ();
Serial.println (nove);
myservo.write (90);
}
if (readString == "centotre")
{
int centotre = Serial.read ();
Serial.println (centotre);
myservo.write (135);
}
if (readString == "centoottanta")
{
int centoottanta = Serial.read ();
Serial.println (centoottanta);
myservo.write (180);
}
if (readString == "blu"){
{
digitalWrite(VERDE, LOW);
digitalWrite(ROSSO, LOW);
digitalWrite(BLU, HIGH);
delay(500);}
}
if (readString == "verde"){
digitalWrite(VERDE, LOW);
digitalWrite(ROSSO, HIGH);
digitalWrite(BLU, LOW);
delay(500);
}
if (readString == "rosso"){
digitalWrite(VERDE, HIGH);
digitalWrite(ROSSO, LOW);
digitalWrite(BLU, LOW);
delay(500);
}
if (readString == "viola"){
digitalWrite(VERDE, HIGH);
digitalWrite(ROSSO, LOW);
digitalWrite(BLU, HIGH);
delay(500);
}
if (readString == "rosa"){
digitalWrite(VERDE, HIGH);
digitalWrite(ROSSO, HIGH);
digitalWrite(BLU, LOW);
delay(500);
}
if (readString == "giallo"){
digitalWrite(VERDE, LOW);
digitalWrite(ROSSO, HIGH);
digitalWrite(BLU, HIGH);
delay(500);
}
if (readString == "bianco"){
digitalWrite(VERDE, HIGH);
digitalWrite(ROSSO, HIGH);
digitalWrite(BLU, HIGH);
delay(500);
}
//esempio con for per cambio colori
/* #define varfija 15
#define retardo 5
for (int contador=0; contador<255; contador++) {
analogWrite(pinLed1, contador);
analogWrite(pinLed2, varfija);
analogWrite(pinLed3, varfija);
delay(retardo);
*/
}
//------------------------------------------------
//---------------------------------------------------
readString="";
}