Help ASAP!

Working on a project and really need some help with the code. Writing to a mega2560 using a sensor to turn on an led.

The problem is, I need the led to trigger only after the analog sensor has given a consecutive block of numbers. This sensor only gives 0s and 1s... So I need the led to trigger only if the sensor has read 1,1,1,1,1.... Not 1,0,0,1. I'm completely new and stumped on how to do this. It would also be great if I could adjust that consecutive block of numbers. Any help would be immensely appreciated.

Working on a project and really need some help with the code. Writing to a mega2560 using a sensor to turn on an led.

The problem is, I need the led to trigger only after the analog sensor has given a consecutive block of numbers. This sensor only gives 0s and 1s... So I need the led to trigger only if the sensor has read 1,1,1,1,1.... Not 1,0,0,1. I'm completely new and stumped on how to do this. It would also be great if I could adjust that consecutive block of numbers. Any help would be immensely appreciated.

I think your description is going to leave a bit to be desired in terms of what exactly are you wanting. Are you wanting the LED to turn on only when the sensor reads a 1 and turn back off when it reads a 0? Or, does it turn on after reading so many 1's? Also, you don't want to use analog for this case, you're going to want to use digital pins seeing as how 1,0 are digital...not analog.

Don't cross post!

Mark

This sensor only gives 0s and 1s

You are describing a digital output from a sensor, not an analogue one. What type of sensor is it ? Can you provide a link to it ?

Hi voodooman. If the sensor is giving out 0 and 1 then its a digital sensor, not analog. Unless it really is analog and you have connected it to a digital input, which is not a good idea, because if the sensor outputs a level in the middle range, the digital input will read 0 or 1 at random. Can you give more detail on this sensor, ideally a link to its data sheet?

Paul

Identical topics merged. Cross-posting is a fast-track to banning - don't do it, IT WASTES TIME.

You take each reading and shift it into a varialbe that holds the last four readings. Use the shift command << then limit the variable to four bits with an and. Finally use the if statement to se if you have the sequence you want. This code is just off the top of my head, not tested

newReading= digitalRead(pin);
record =( record << 1) | (newReading & 1);
record &= 0xF;
If ( record == 0xF) {
// do your stuff
}

That is for four bits, or four previous readings change the AND operation. Number for more bits.

The sensor is a parallax x-band sensor. When reading it plugged into analog port 0, the serial monitor at 9600 baud displays 0 when no motion is detected and 1s when motion is detected. However, because this sensor is so immensely sensitive, it will pulse a "1" if a spec of dust floats by it. I'm more interested in triggering an event such as turning on an LED light or flipping on a relay from large objects passing it such as a car. This will basically filter out the small things and I'm just stumped on how to do it.

What amplification have you got between the sensor and the analogue pin? Those things put out microvolts .

When reading it plugged into analog port 0, the serial monitor at 9600 baud displays 0 when no motion is detected and 1s when motion is detected.

So there is code that does that. So as in all cases post the code you have.

Okay, no laughing. I tried plugging in a second x-band sensor to see if it output the same way and the result was quite different. Now I believe I am seeing voltage. On serial monitor it will read 0-700… when im running around the room the sensor seems to read in the upper 600s. So I guess my real need is to take some kind of running average and when a thresh hold has been crossed, I want to activate a relay or an LED on port 13. Here is my terrible frankenstein code:

int calibrationTime = 15;   

int sensorpin = 0;                 // analog pin used to connect the radar
int val = 0;                 // variable to store the values from sensor(initially zero)
int ledPin = 13; // choose the pin for the LED 
const int threshold = 1000;   // an arbitrary threshold level that's in the range of the analog input
const int analogPin = 0;    // pin that the sensor is attached to

void setup()
{
/////////////////////////////
//SETUP
  Serial.begin(9600);               // starts the serial monitor

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(500);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
}

 
void loop()
{
  val = digitalRead(sensorpin);       // reads the value of the sharp sensor
  Serial.println(val);            // prints the value of the sensor to the serial monitor
  delay(100);                    // wait for this much time before printing next value
  
 
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
     Serial.println("Triggered");
     delay(0); 
  } 
  else {
    digitalWrite(ledPin,LOW); 
  }

  // print the analog value:
  Serial.println(analogValue, DEC);

}

As I said, those things put out Doppler frequencies measured in microvolts.

Yep, you were/are right about that. I just had a defective unit. Luckily I had a backup. Do you have any clue how to implement some kind of average/threshhold trigger?

I was just reading about this code:

const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup()

Does anyone know how I would implement it exactly into the previous code i posted?

I suppose the other problem I'm going to run into is that a running average of a low number will cause false trigger. Is there a way to set a thresh hold for a running average that will then cause a trigger? Say, if the sensor is detecting anything below 300 microvolts, then don't fire, but if it is detecting somewhere close to the expected range of a large object, "say 500-800 microvolts" then trigger away?!?!

Help!

Here is my most updated version of the code I’m trying to use:

int calibrationTime = 15;   

int sensorpin = 0;                 // analog pin used to connect the radar
int val = 0;                 // variable to store the values from sensor(initially zero)
int ledPin = 13; // choose the pin for the LED 
const int threshold = 300;   // an arbitrary threshold level that's in the range of the analog input
const int analogPin = 0;    // pin that the sensor is attached to

// AVERAGING PROTOCOL CODE
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          
}

void loop() {
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = total / numReadings;         
  
  // triggering protocol
  // if the analog value is high enough, turn on the LED:
  if (average > threshold) {
    digitalWrite(ledPin, HIGH);
  } 
  else {
    digitalWrite(ledPin,LOW); 
  }
  // send it to the computer as ASCII digits
  Serial.println(average);   
  delay(75);        // delay in between reads for stability            
}

It’s not tripping the LED for some reason… I don’t know what I’m doing. Please help.

I can't see where you define the LED pin to be an output, this should be in the setup function.

Hi again voodooman. You really need to post a link to this sensor's data sheet. I did a search for parrallax x band sensor and read a data sheet. The only one I found does not output an analog voltage, it outputs a series of digital pulses that you count over a half second period, for example.

Paul