Ciao a tutti ,starei cercando di mandare da aruino a max 2 diversi flussi di dati ,uno da un sensore ultrasuoni ed uno da una fotoresistenza contemporaneamente .
sono arrivato a questo punto :
#define echoPin 2 // Pin to receive echo pulse
#define trigPin 3 // Pin to send trigger pulse
//PhotoResistor Pin
int lightPin = 0; //the analog pin the photoresistor is
//connected to
//the photoresistor is not calibrated to any units so
//this is simply a raw sensor value (relative light)
//LED Pin
int ledPin = 9; //the pin the LED is connected to
//we are controlling brightness so
//we use one of the PWM (pulse width
// modulation pins)
void setup()
{
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(ledPin, OUTPUT); //sets the led pin to output
}
/*
- loop() - this function will start after setup
- finishes and then repeat
*/
void loop()
{
int lightLevel = analogRead(lightPin); //Read the
// lightlevel
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to
//span 0 to 255
lightLevel = constrain(lightLevel, 0, 255);//make sure the
//value is betwween
//0 and 255
analogWrite(ledPin, lightLevel); //write the value
digitalWrite(trigPin, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // Send pin low again
int distance = pulseIn(echoPin, HIGH); // Read in times pulse
distance= distance/58; // Calculate distance from time of pulse
Serial.write(distance);
delay(50); // Wait 50mS before next ranging
Serial.write(1);
Serial.write(lightLevel);
// Serial.write(ultrasoundlevel);
ma adesso mi sono incartato..sarà l'ora..
secondo voi è giusto così?
grazie