X6 Reflective Switch to switch X6 LED's and Data Logger

Hi all,

I'm all new to Arduino programming and my electronics is fairly basic too. I'm struggling on a project to detect if a weight is dropped when a device has failed. I'm using a data logger to read the voltage outputs, this setup is used when the weight is no longer present, i.e. the beam is broken when the weight is present. The problems I'm finding are the outputs are not showing the desired outputs I would like. I need a digital=HIGH 5V; for when a weight is there and a digital=LOW 0V; for when it isn't, much like how a straightforward switch works. 5volts would be nice as the output for each channel/sensor to LED to work independently.

Sensors: (OPB720B-06Z) http://docs-europe.electrocomponents.com/webdocs/0b08/0900766b80b08c06.pdf

Here is the code I used and tweaked from: Examples/Analog/AnalogInOutSerial. I know it's analogue, and with this code used the setup works but it is not reliable. I need a way to set sensitivity too if possible?

Adam,

const int analogInPin1 = A0;
const int analogInPin2 = A1;
const int analogInPin3 = A2;
const int analogInPin4 = A3;
const int analogInPin5 = A4;
const int analogInPin6 = A5;

const int analogOutPin1 = 2;
const int analogOutPin2 = 3;
const int analogOutPin3 = 4;
const int analogOutPin4 = 5;
const int analogOutPin5 = 6;
const int analogOutPin6 = 7;

int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int sensorValue6 = 0;

int outputValue1 = 0;
int outputValue2 = 0;
int outputValue3 = 0;
int outputValue4 = 0;
int outputValue5 = 0;
int outputValue6 = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin3);
sensorValue4 = analogRead(analogInPin4);
sensorValue5 = analogRead(analogInPin5);
sensorValue6 = analogRead(analogInPin6);
// map it to the range of the analog out:
outputValue1 = map(sensorValue1, 0, 1023, 0, 255);
outputValue2 = map(sensorValue2, 0, 1023, 0, 255);
outputValue3 = map(sensorValue3, 0, 1023, 0, 255);
outputValue4 = map(sensorValue4, 0, 1023, 0, 255);
outputValue5 = map(sensorValue5, 0, 1023, 0, 255);
outputValue6 = map(sensorValue6, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin1, outputValue1);
analogWrite(analogOutPin2, outputValue2);
analogWrite(analogOutPin3, outputValue3);
analogWrite(analogOutPin4, outputValue4);
analogWrite(analogOutPin5, outputValue5);
analogWrite(analogOutPin6, outputValue6);

}

Are you sure about the pins for those analogWrites?

Hi,

Thanks for the reply! Could this be where I'm going wrong?

I'm using an Arduino Mega 2560

  outputValue1 = map(sensorValue1, 0, 1023, 0, 255);

A not very efficient way to divide by 4.

If you need a nice 5V or a nice 0V output, why are you using PWM?

Which Arduino are you using? Not all pins support PWM. Which ones do depends on which Arduino you have.

I'm using an Arduino Mega 2560, Pin's 2 to 7 "in use" are PWM marked on the PCB :slight_smile:

How about the other question? If you want 0V or 5V, PWM is not the way to get it.

Ok, I understand now, thank you :slight_smile:

I have changed the pin's to 52, 50, 48, 46, 44 and 42. But I'm only getting 2.7V from each channel and the LED's are not Lighting up. What now?

Thanks,

Adam

What now?

Post the code. Post a schematic. Add some Serial.print() statements to see what is being received. Lots of things you could do.

Keep in mind that we can't see what you are doing, what you are measuring, how anything is connected, what you are trying to do, etc. We can only go on what you post, and you aren't posting very much.

I can't get any pictures on here as I can't log into my photobucket account. I have listed the sensors I'm using at the start of the thread. No pull-up resisters are in use as I don't think they are needed. 5volt pin connected (+red) with Ground (-black)[all six] and the Open collector is connected to A0 (white) A1 A2 A3 and so on for each sensor. The LED's are connected a similar way (-ground) and positive is reflected on each pin I have used, 52, 50, 48, 46, 44 and 42.

With this code uploaded nothing happens...

I have very very little xp with code/programming if any, I can some how see the logic in the code but, that's all... All I really need is some help with the code to cut and paste, upload.. Hand over sensor LED "ON"... No hand LED "OFF" surely it's a simple few lines? :frowning: If it's to much and loads of code needs to be written I can just scrap the Arduino board and try opto isolators with relays.. :slight_smile: Job done...

const int buttonPin1 = A0; // the number of the pushbutton pin
const int buttonPin2 = A1;
const int buttonPin3 = A2;
const int buttonPin4 = A3;
const int buttonPin5 = A4;
const int buttonPin6 = A5;
const int ledPin1 = 52; // the number of the LED pin
const int ledPin2 = 50;
const int ledPin3 = 48;
const int ledPin4 = 46;
const int ledPin5 = 44;
const int ledPin6 = 42;

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
buttonState6 = digitalRead(buttonPin6);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:

//1
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);

}
//2
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin2, LOW);
//3
}
if (buttonState3 == HIGH) {
// turn LED on:
digitalWrite(ledPin3, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin3, LOW);
//4
}
if (buttonState4 == HIGH) {
// turn LED on:
digitalWrite(ledPin4, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin4, LOW);
//5
}
if (buttonState5 == HIGH) {
// turn LED on:
digitalWrite(ledPin5, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin5, LOW);
}
//6
if (buttonState6 == HIGH) {
// turn LED on:
digitalWrite(ledPin6, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin6, LOW);
}
}

I have try’d the way too and the LED’s don’t independently light up…

const int sensorPin1 = A0; // pin that the sensor is attached to
const int sensorPin2 = A1;
const int sensorPin3 = A2;
const int sensorPin4 = A3;
const int sensorPin5 = A4;
const int sensorPin6 = A5;
const int ledPin1 = 52; // pin that the LED is attached to
const int ledPin2 = 50;
const int ledPin3 = 48;
const int ledPin4 = 46;
const int ledPin5 = 44;
const int ledPin6 = 42;

// variables:
int sensorValue1 = 0; // the sensor value
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int sensorValue6 = 0;
int sensorMin1 = 1023; // minimum sensor value
int sensorMin2 = 1023;
int sensorMin3 = 1023;
int sensorMin4 = 1023;
int sensorMin5 = 1023;
int sensorMin6 = 1023;
int sensorMax1 = 0; // maximum sensor value
int sensorMax2 = 0;
int sensorMax3 = 0;
int sensorMax4 = 0;
int sensorMax5 = 0;
int sensorMax6 = 0;

void setup() {
// turn on LED to signal the start of the calibration period:
pinMode(52, OUTPUT);
pinMode(50, OUTPUT);
pinMode(48, OUTPUT);
pinMode(46, OUTPUT);
pinMode(44, OUTPUT);
pinMode(42, OUTPUT);
digitalWrite(52, HIGH);
digitalWrite(50, HIGH);
digitalWrite(48, HIGH);
digitalWrite(46, HIGH);
digitalWrite(44, HIGH);
digitalWrite(42, HIGH);

// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
sensorValue4 = analogRead(sensorPin4);
sensorValue5 = analogRead(sensorPin5);
sensorValue6 = analogRead(sensorPin6);

// record the maximum sensor value

//1
if (sensorValue1 > sensorMax1) {
sensorMax1 = sensorValue1;
}

// record the minimum sensor value
if (sensorValue1 < sensorMin1) {
sensorMin1 = sensorValue1;
}

//2
if (sensorValue2 > sensorMax2) {
sensorMax2 = sensorValue2;
}

// record the minimum sensor value
if (sensorValue2 < sensorMin2) {
sensorMin2 = sensorValue2;
}

//3
if (sensorValue3 > sensorMax3) {
sensorMax3 = sensorValue3;
}

// record the minimum sensor value
if (sensorValue3 < sensorMin3) {
sensorMin3 = sensorValue3;
}

//4
if (sensorValue4 > sensorMax4) {
sensorMax4 = sensorValue4;
}

// record the minimum sensor value
if (sensorValue4 < sensorMin4) {
sensorMin4 = sensorValue4;
}
//5
if (sensorValue5 > sensorMax5) {
sensorMax5 = sensorValue5;
}

// record the minimum sensor value
if (sensorValue5 < sensorMin5) {
sensorMin5 = sensorValue5;
}

//6
if (sensorValue6 > sensorMax6) {
sensorMax6 = sensorValue6;
}

// record the minimum sensor value
if (sensorValue6 < sensorMin6) {
sensorMin6 = sensorValue6;
}
}

// signal the end of the calibration period
digitalWrite(52, LOW);
digitalWrite(50, LOW);
digitalWrite(48, LOW);
digitalWrite(46, LOW);
digitalWrite(44, LOW);
digitalWrite(42, LOW);
}

void loop() {
// read the sensor:
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
sensorValue4 = analogRead(sensorPin4);
sensorValue5 = analogRead(sensorPin5);
sensorValue6 = analogRead(sensorPin6);

// apply the calibration to the sensor reading
sensorValue1 = map(sensorValue1, sensorMin1, sensorMax1, 0, 255);
sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, 0, 255);
sensorValue3 = map(sensorValue3, sensorMin3, sensorMax3, 0, 255);
sensorValue4 = map(sensorValue4, sensorMin4, sensorMax4, 0, 255);
sensorValue5 = map(sensorValue5, sensorMin5, sensorMax5, 0, 255);
sensorValue6 = map(sensorValue6, sensorMin6, sensorMax6, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue1 = constrain(sensorValue1, 0, 255);
sensorValue2 = constrain(sensorValue2, 0, 255);
sensorValue3 = constrain(sensorValue3, 0, 255);
sensorValue4 = constrain(sensorValue4, 0, 255);
sensorValue5 = constrain(sensorValue5, 0, 255);
sensorValue6 = constrain(sensorValue6, 0, 255);

// fade the LED using the calibrated value:
analogWrite(ledPin1, sensorValue1);
analogWrite(ledPin2, sensorValue2);
analogWrite(ledPin3, sensorValue3);
analogWrite(ledPin4, sensorValue4);
analogWrite(ledPin5, sensorValue5);
analogWrite(ledPin6, sensorValue6);
}

Rather than relying on undebugged hardware to indicate success or failure of your code, why don't you use the serial interface to give an idea of the the progress of your code?

Once you've got your code working for one sensor and one LED (hint) it should be relatively simple to expand it to as many as you want (Hint: arrays).

if (buttonState1 == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin1, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);

If "buttonState" can only be HIGH, or LOW, then    digitalWrite(ledPin1, buttonState1;
will reduce the amount you have to type a huge amount.

Can you start using code boxes for your code please?
My scrolling finger hurts.

I have used serial, it just streams out numbers witch don't really help at all when I move my hand over the sensor/s, I've just been using a multimeter instead. I hardly use forums, how do I make a code box? :slight_smile:

Nothing Happens :expressionless:

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, buttonState); 
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

Sorry I lie :blush:, there is a lag/delay when my hand covers the sensor, I'm happy with this but if there is a way to help with the latency is would be nice :slight_smile:

if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, buttonState); 
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

No, you misunderstand.
You don't need the "if" at all.

If I remove "if" I get this

Arduino_003.cpp: In function 'void loop()':
Arduino_003:52: error: expected `;' before '{' token
Arduino_003:56: error: 'else' without a previous 'if'

This is how the program looks now

const int SensorPin1 = A0;     // the number of the pushbutton pin
const int SensorPin2 = A1;
const int SensorPin3 = A2;
const int SensorPin4 = A3;
const int SensorPin5 = A4;
const int SensorPin6 = A5;
const int LEDPin1 =  52;      // the number of the LED pin
const int LEDPin2 =  50;
const int LEDPin3 =  48;
const int LEDPin4 =  46;
const int LEDPin5 =  44;
const int LEDPin6 =  42;

// variables will change:
int SensorState1 = 1;         // variable for reading the pushbutton status
int SensorState2 = 1;
int SensorState3 = 1;
int SensorState4 = 1;
int SensorState5 = 1;
int SensorState6 = 1;

void setup() {
  Serial.begin(9600); 
  // initialize the LED pin as an output:
  pinMode(LEDPin1, OUTPUT);     
  pinMode(LEDPin2, OUTPUT);
  pinMode(LEDPin3, OUTPUT);
  pinMode(LEDPin4, OUTPUT);
  pinMode(LEDPin5, OUTPUT);
  pinMode(LEDPin6, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(SensorPin1, INPUT);     
  pinMode(SensorPin2, INPUT);
  pinMode(SensorPin3, INPUT);
  pinMode(SensorPin4, INPUT);
  pinMode(SensorPin5, INPUT);
  pinMode(SensorPin6, INPUT);
}

void loop(){
  // read the state of the pushbutton value:
  SensorState1 = digitalRead(SensorPin1);
  SensorState2 = digitalRead(SensorPin2);
  SensorState3 = digitalRead(SensorPin3);
  SensorState4 = digitalRead(SensorPin4);
  SensorState5 = digitalRead(SensorPin5);
  SensorState6 = digitalRead(SensorPin6);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  //1
  if (SensorState1 == HIGH) {     
    // turn LED on:    
    digitalWrite(LEDPin1, SensorState1); 
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPin1, LOW); 

  }

  //2
  if (SensorState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(LEDPin2, SensorState2); 
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPin2, LOW); 

  }

  //3
  if (SensorState3 == HIGH) {     
    // turn LED on:    
    digitalWrite(LEDPin3, SensorState3); 
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPin3, LOW); 

  }

  //4
  if (SensorState4 == HIGH) {     
    // turn LED on:    
    digitalWrite(LEDPin4, SensorState4); 
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPin4, LOW); 

  }

  //5
  if (SensorState5 == HIGH) {     
    // turn LED on:    
    digitalWrite(LEDPin5, SensorState5); 
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPin5, LOW); 

  }

  //6
  if (SensorState6 == HIGH) {     
    // turn LED on:    
    digitalWrite(LEDPin6, SensorState6); 
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPin6, LOW); 

  }
  Serial.print("sensor1 = " );                       
  Serial.print(SensorState1);      
  Serial.print("\t output1 = ");      
  Serial.println(LEDPin1);  
}

Is there some way in increasing the sensitivity and getting rid of the lag?

if (SensorState5 == HIGH) {     
    // turn LED on:    
    digitalWrite(LEDPin5, SensorState5); 
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPin5, LOW); 

  }

is just the same as writing (and ONLY writing) digitalWrite(LEDPin5, SensorState5);

Is there some way in increasing the sensitivity and getting rid of the lag?

Sure. Use the right kind of sensor. There is nothing that the Arduino is doing that affects sensitivity - that's entirely up to the sensor. The Arduino is reading the sensor status often enough. If it takes your sensor a long time to react, there is nothing the Arduino can do to speed it up.