Loading...
Pages: [1]   Go Down
Author Topic: sharp ir switch  (Read 631 times)
0 Members and 1 Guest are viewing this topic.
Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi everyone,after reading allmost all post around here and google,hope 2 get my answers here.I found hundred of examples of Ir conected to Atmegas that light up a Led when get close to object,but not even one example on how to make a toogle switch.Im not the only one looking for codes like this,truth is this subject is avoided,anyway..I wanna do a simple toogle switch,with the arduino Duemilanove and the Sharp 2Y0A21 ir sensor,that can light a Led.So far i can only make it like momentary switch:
+LED on pin 13
+DATA from ir on pin analog 0
----------------------------------
int led1 = 13;
int sensor = A0;
 
void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);     
}
 
void loop() {
// we read the sensor Value
  int Value = analogRead(A0);
// we maps the value
  int sensorValue = map(Value, 0, 1024, 0 , 200);
// turn on the led1 when the value is beetween 50 and 100
  if ( sensorValue > 50 && sensorValue <300){
    digitalWrite(13, HIGH);
 
  }

  else {
// turn off the two leds
    digitalWrite(13, LOW);
   
  }
  Serial.println(sensorValue);
  delay(100);
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 96
Posts: 6347
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int led1 = 13;
const int sensor = A0;
 
void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);    
}
 
void loop() {
  static boolean lastSensorHit = false;
  static boolean LEDvalue = LOW;

  int sensorValue = map(analogRead(sensor), 0, 1024, 0 , 200);
  bool sensorHit = sensorValue > 50 && sensorValue <300;

  if (sensorHit && !lastSensorHit)  // Now true, was false
    {
    LEDvalue = !LEDvalue; // Toggle the LED
    digitalWrite(led1, LEDvalue);
    }
  lastSensorHit = sensorHit;
  delay(100);
}
« Last Edit: October 18, 2012, 09:10:48 am by johnwasser » Logged

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

work like charm,no debounce and very stable,thank you so much johnwasser,i realy apreciate your help smiley-grin
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I make program using analog sound sensor and DHT22 humidity and temperature sensor. When I see serial monitor, it print sensor value so slowly. In this case the analog sound sensor become not responsive. When I use only analog sound sensor (without DHT22), serial monitor printed analog sensor value so fast. Is there a problem with DHT22 program?

#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT22   // DHT 22  (AM2302)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
 
  Serial.begin(9600);
  Serial.println("DHTxx test!");
 
  dht.begin();
}

void loop() {
   
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.println("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
  }
   int sensorValue = analogRead(A0);//use A0 to read the electrical signal
  Serial.println(sensorValue);
}
Logged

Pages: [1]   Go Up
Print
 
Jump to: