I am just trying to set up a device using infrared sensors to trigger some LEDs.
Im just struggling with my coding in that i have programmed 2 digital pins, 3 and 4 to DigitalpinNext and Previous (as shown later in the code) linked to SensorRightPin and LeftPin. But with the code below (it follows on from other coding thus why there is no Loop in what i have shown you):
// Left sensor and 'Previous' LED
int sensorLeftValue = digitalRead(sensorRightPin);
if(sensorLeftValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinPrevious, HIGH);
}
else {
digitalWrite(ledPinPrevious, LOW);
}
//Right sensor and 'Next' LED
int sensorRightValue = digitalRead(sensorLeftPin);
if(sensorRightValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinNext, HIGH);
}
else {
digitalWrite(ledPinNext, LOW);
}
}
It seems to skip the 2nd set of code, in this case Right sensor and Next LED. I have swapped the 'sets' of code around and the first set is always working but the 2nd seems to just be ignored.
It is as though i need to put the code in Parrallel? I dont know if this principle is possible. As though i need them to register at the same time and currently the 2nd set is overidden by the 1st set.
I have check all the LEDs and combinations by changing the LEDs and Sensors into different pins on the arduino board.
Does anyone have any idea? It would be really helpful as i am fairly inexperienced and rushed for time
/*
Infrared sensors
5 total.
Play/pause Zone, 5-6cm (roughly), Red LED
Volume zone, 0-12cm, Row of 6 LEDs
Skip, 2 sensors, left and right, 2 LEDs, green and orange. (need sequence added)
Function sensor, 0-5 cm (roughly), dim Red LED.
*/
// these constants won't change:
const int analogPinVol = 0; // the pin that the volume is attached to
const int ledCount = 6; // the number of LEDs in the bar graph
const int analogPinPlay = 1; // the pin that the play/pause is attached to
const int analogPinSource = 2; // the pin that the source is attached to
const int ledPinPlay = 6; // the pin that the LED for Play/pause is attached to
const int ledPinSource = 5; // the pin that the LED for source is attached to
const int ledPinPrevious = 3; // the pin that the LED for Previous track is attached to
const int ledPinNext = 4; // the pin that the LED for Next track is attached to
// Variables will change:
int ledPins[] = {
7, 9, 10, 11, 12, 13 }; // an array of pin numbers to which LEDs are attached
int val = 0;
int valSource = 0;
boolean sensorLeftTriggered = false;
boolean sensorRightTriggered = false;
int sensorLeftPin = 1;
int sensorRightPin = 2;
long sensorLeftTime = 0;
long sensorRightTime = 0;
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
pinMode(ledPinPlay, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinSource, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinPrevious, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinNext, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(sensorLeftPin, INPUT); // declare the sensorleftpin as INPUT
pinMode(sensorRightPin, INPUT); // declare the sensorrightpin as INPUT
}
}
void loop() {
// first handle the led bar
// read the volume:
int sensorReading = analogRead(analogPinVol);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 140, 620, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
// then do the Play Led
val = analogRead(analogPinPlay);
if ((val > 500) && (val < 600)) {
digitalWrite(ledPinPlay, HIGH);
}
else {
//
digitalWrite(ledPinPlay, LOW);
}
// then do the Source Led
valSource = analogRead(analogPinSource);
if (valSource > 500) {
digitalWrite(ledPinSource, HIGH);
}
else {
//
digitalWrite(ledPinSource, LOW);
}
// Left sensor and 'Previous' LED
int sensorLeftValue = digitalRead(sensorRightPin);
if(sensorLeftValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinPrevious, HIGH);
}
else {
digitalWrite(ledPinPrevious, LOW);
}
//Right sensor and 'Next' LED
int sensorRightValue = digitalRead(sensorLeftPin);
if(sensorRightValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinNext, HIGH);
}
else {
digitalWrite(ledPinNext, LOW);
}
}
Its quite jumbled in parts as i am trying loads of stuff out
int sensorLeftValue = digitalRead([glow]sensorRightPin[/glow]);
if(sensorLeftValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinPrevious, HIGH);
}
else {
digitalWrite(ledPinPrevious, LOW);
}
//Right sensor and 'Next' LED
int sensorRightValue = digitalRead([glow]sensorLeftPin[/glow]);
Is this intentional?
(BTW, you can auto-format your code in the IDE to make it more readable)
It is intentional yeah as SensorRightPin is one sensor which i want to light up one LED and then SensorLeftPin is the other sensor which i want to light up a different LED when triggered.
I havent looked at this coding for a while as its been a few weeks over christmas but i think ive set this up like this as in the future i planned to have the 2 sensors left and right become a sequence. So depending on which sensor was triggered first it would light up the corresponding LED. For example if left was triggered before right then a red LED is lit, right before left, green LED lit.
I dont know whether to just try and set up that sequence now, as i felt i should fix what is happening now...
I have this code:
boolean sensorLeftTriggered = false;
boolean sensorRightTriggered = false;
int sensorLeft = LOW;
int sensorRight = LOW;
long sensorLeftTime = 0;
long sensorRightTime = 0;
int sensorLeftPin = 2;
int sensorRightPin = 3;
void setup()
{
pinMode(sensorLeftPin, INPUT);
pinMode(sensorRightPin, INPUT);
}
void loop()
{
int sensorLeftValue = digitalRead(sensorLeftPin);
int sensorRightValue = digitalRead(sensorRightPin);
if(sensorLeftValue == HIGH && sensorLeftValue != sensorLeft)
{
// Sensor triggered, and was not triggered before
sensorLeftTime = millis();
sensorLeftTriggered = true;
}
sensorLeft = sensorLeftValue;
if(sensorRightValue == HIGH && sensorRightValue != sensorRight)
{
// Sensor triggered, and was not triggered before
sensorRightTime = millis();
sensorRightTriggered = true;
}
sensorRight = sensorRightValue;
// See if both sensors have been triggered
if(sensorLeftTriggered && sensorRightTriggered)
{
if(sensorLeftTime - sensorRightTime > 0)
{
// Sensor right was triggered, then sensor left
}
else
{
// Sensor left was triggered, then sensor right
}
}
}
As a basis for these two switches but it doesnt seem to work. I feel like there are huge gaps i need to fill with information but being an amature i dont kow what to fill?
ahh i see, yeah i think that is were i have just been testing the sensors... making sure both left and right work in the first set. Which they do... it doesnt really matter which way around they are...
more the issue as to why it only uses the first set :s as though it just stops before it 'reads' the next set of code
/*
Infrared sensors
5 total.
Play/pause Zone, 5-6cm (roughly), Red LED
Volume zone, 0-12cm, Row of 6 LEDs
Skip, 2 sensors, left and right, 2 LEDs, green and orange. (need sequence added)
Function sensor, 0-5 cm (roughly), dim Red LED.
*/
// these constants won't change:
const int ledPinPrevious = 3; // the pin that the LED for Previous track is attached to
const int ledPinNext = 4; // the pin that the LED for Next track is attached to
boolean sensorLeftTriggered = false;
boolean sensorRightTriggered = false;
int sensorLeftPin = 1;
int sensorRightPin = 2;
long sensorLeftTime = 0;
long sensorRightTime = 0;
void setup() {
pinMode(ledPinPrevious, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinNext, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(sensorLeftPin, INPUT); // declare the sensorleftpin as INPUT
pinMode(sensorRightPin, INPUT); // declare the sensorrightpin as INPUT
Serial.begin(1200);
}
void loop() {
// Left sensor and 'Previous' LED
int sensorLeftValue = digitalRead(sensorLeftPin);
Serial.println(sensorLeftValue, DEC);
if(sensorLeftValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinPrevious, HIGH);
}
else {
digitalWrite(ledPinPrevious, LOW);
}
//Right sensor and 'Next' LED
int sensorRightValue = digitalRead(sensorRightPin);
if(sensorRightValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinNext, HIGH);
}
else {
digitalWrite(ledPinNext, LOW);
}
}
I get a reading of 0 continuously and no LED lighting up. Without the Serial.begin(1200) and Serial.printIn(sensorLeftValue, Dec) the coding works fine :s
I have determined its the case that only digital pin 1 is working with the digital sensors. I have an Arduino Duemilanove and if i put the sensors into say Pin 2 and 6 like so:
/*
Infrared sensors
5 total.
Play/pause Zone, 5-6cm (roughly), Red LED
Volume zone, 0-12cm, Row of 6 LEDs
Skip, 2 sensors, left and right, 2 LEDs, green and orange. (need sequence added)
Function sensor, 0-5 cm (roughly), dim Red LED.
*/
// these constants won't change:
const int ledPinPrevious = 3; // the pin that the LED for Previous track is attached to
const int ledPinNext = 4; // the pin that the LED for Next track is attached to
boolean sensorLeftTriggered = false;
boolean sensorRightTriggered = false;
int sensorLeftPin = 6;
int sensorRightPin = 2;
long sensorLeftTime = 0;
long sensorRightTime = 0;
void setup() {
pinMode(ledPinPrevious, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinNext, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(sensorLeftPin, INPUT); // declare the sensorleftpin as INPUT
pinMode(sensorRightPin, INPUT); // declare the sensorrightpin as INPUT
}
void loop() {
// Left sensor and 'Previous' LED
int sensorLeftValue = digitalRead(sensorRightPin);
if(sensorLeftValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinPrevious, HIGH);
}
else {
digitalWrite(ledPinPrevious, LOW);
}
//Right sensor and 'Next' LED
int sensorRightValue = digitalRead(sensorLeftPin);
if(sensorRightValue == HIGH) {
// Sensor triggered, and was not triggered before
digitalWrite(ledPinNext, HIGH);
}
else {
digitalWrite(ledPinNext, LOW);
}
}
I get no LEDs being lit. This seems very odd as i have had these digital pins taking readings before :s.
Also if i try to set one of the digital pins to 0 it wont upload the code.
This would explain why only one LED was coming on as only pin 1 and 2 were being used... thus meaning what ever sensor was plugged into 1 was taking the results and lighting the corresponding LED.
Have these issues got something to do with the TX> and RX< on the Duemilanove boards?
I was under the impression you could plug a digital sensor into any of the digital pin and take in readings, is this not the case?
Digital pins 0 and 1 are used for serial communication, which includes uploading sketches. If you don't need that capability (which you do), you can use them for digital input or output.
All 13 digital pins should work with any digital sensor or digital output device.
:S. It doesnt make sense that any other pin wouldnt work. If i simply change the pin number to 2 in the code and connect the same sensor that was being used in pin 1 to pin 2 it just doesnt work. i get nothing.
Ok i think i have established that it is a hardware issue and therefore for some reason only pin 1 seems to take in a digital sensor reading.
This howevere isnt an issue as with these two digital sensors i was merely going to show the principle.
i aim aiming to use 2 analogue sensors as i can manage the range of which creates an output, such as lighting an LED.
The issue i have now is changing this sequence code:
boolean sensorLeftTriggered = false;
boolean sensorRightTriggered = false;
int sensorLeft = LOW;
int sensorRight = LOW;
long sensorLeftTime = 0;
long sensorRightTime = 0;
int sensorLeftPin = 2;
int sensorRightPin = 3;
void setup()
{
pinMode(sensorLeftPin, INPUT);
pinMode(sensorRightPin, INPUT);
}
void loop()
{
int sensorLeftValue = digitalRead(sensorLeftPin);
int sensorRightValue = digitalRead(sensorRightPin);
if(sensorLeftValue == HIGH && sensorLeftValue != sensorLeft)
{
// Sensor triggered, and was not triggered before
sensorLeftTime = millis();
sensorLeftTriggered = true;
}
sensorLeft = sensorLeftValue;
if(sensorRightValue == HIGH && sensorRightValue != sensorRight)
{
// Sensor triggered, and was not triggered before
sensorRightTime = millis();
sensorRightTriggered = true;
}
sensorRight = sensorRightValue;
// See if both sensors have been triggered
if(sensorLeftTriggered && sensorRightTriggered)
{
if(sensorLeftTime - sensorRightTime > 0)
{
// Sensor right was triggered, then sensor left
}
else
{
// Sensor left was triggered, then sensor right
}
}
}
i believe you gave me this code back before christmas but i always struggled to get it working. Now i need to get it to work with analogue sensors im even more confused.
What would i have to change? I dont really understand the boolean element either?
The problem with using analog sensors in the "which way did they go" scenario is that you'll need to define a threshold which defines when someone/something went "by" the sensors.
Then, you change the digitalRead to analogRead, and the pins that the sensors are attached to.
Change the "== HIGH" to ">= threshold", and get rid of the " && sensorLeftValue != sensorLeft" bit. Do the same for the right sensor.
The actions that happen when both sensors have been tripped, and the chec of the order that they were triggered remains the same.
So far i have mocked up this code but it doesnt seem to be right. I feel the threshold is right and i have set it up in the same way as setting up a value with my previous code but im a little confused on the last section when it trys to determine what is triggered. I feel like i am missing a section...
My code so far is below:
/*
Infrared sensors
skip and previous with 2 analog sensors with 140 as a threshold.
*/
// these constants won't change:
const int ledPinPrevious = 3; // the pin that the LED for Previous track is attached to
const int ledPinNext = 4; // the pin that the LED for Next track is attached to
const int analogsensorLeft = 1; // the pin that the play/pause is attached to
const int analogsensorRight = 2; // the pin that the source is attached to
boolean sensorLeftTriggered = false;
boolean sensorRightTriggered = false;
int sensorLeft = LOW;
int sensorRight = LOW;
long sensorLeftTime = 0;
long sensorRightTime = 0;
int threshold = 140
void setup()
{
pinMode(ledPinPrevious, OUTPUT);
pinMode(ledPinNext, OUTPUT);
}
void loop()
{
int sensorLeftValue = analogRead(analogsensorLeft);
int sensorRightValue = analogRead(analogsensorRight);
if(sensorLeftValue >= threshold)
{
// Sensor triggered, and was not triggered before
sensorLeftTime = millis();
sensorLeftTriggered = true;
}
sensorLeft = sensorLeftValue;
if(sensorRightValue >= threshold)
{
// Sensor triggered, and was not triggered before
sensorRightTime = millis();
sensorRightTriggered = true;
}
sensorRight = sensorRightValue;
// See if both sensors have been triggered
if(sensorLeftTriggered && sensorRightTriggered)
{
if(sensorLeftTime - sensorRightTime > 0) {
digitalWrite(ledPinPrevious, HIGH)
// Sensor right was triggered, then sensor left
}
else
{
digitalWrite(ledPinNext, HIGH); // Sensor left was triggered, then sensor right
}
}
I dont know if the if statements are right? when i try to verify it says error: expected unqualified-id before numeric constant.
ahh yeah.. sorry, definietely missed that one. Also corrected a few other missing ; :).
Ive now got this:
/*
Infrared sensors
skip and previous with 2 analog sensors with 140 as a threshold.
*/
// these constants won't change:
const int ledPinPrevious = 3; // the pin that the LED for Previous track is attached to
const int ledPinNext = 4; // the pin that the LED for Next track is attached to
const int analogsensorLeft = 1; // the pin that the play/pause is attached to
const int analogsensorRight = 2; // the pin that the source is attached to
boolean sensorLeftTriggered = false;
boolean sensorRightTriggered = false;
int sensorLeft = LOW;
int sensorRight = LOW;
long sensorLeftTime = 0;
long sensorRightTime = 0;
int threshold = 140;
void setup()
{
pinMode(ledPinPrevious, OUTPUT);
pinMode(ledPinNext, OUTPUT);
}
void loop()
{
int sensorLeftValue = analogRead(analogsensorLeft);
int sensorRightValue = analogRead(analogsensorRight);
if(sensorLeftValue >= threshold)
{
// Sensor triggered, and was not triggered before
sensorLeftTime = millis();
sensorLeftTriggered = true;
}
sensorLeft = sensorLeftValue;
if(sensorRightValue >= threshold)
{
// Sensor triggered, and was not triggered before
sensorRightTime = millis();
sensorRightTriggered = true;
}
sensorRight = sensorRightValue;
// See if both sensors have been triggered
if(sensorLeftTriggered && sensorRightTriggered)
if(sensorLeftTime - sensorRightTime > 0) {
digitalWrite(ledPinPrevious, HIGH);
// Sensor right was triggered, then sensor left
}
else {
digitalWrite(ledPinNext, HIGH); // Sensor left was triggered, then sensor right
}
}
It compiles fine and seems to all work but when uploaded the 2 LEDs are permanently on. Is it not a case that i need to tell the LEDs to be in a low state somewhere?
int sensorLeft = LOW;
int sensorRight = LOW;
Is this section only relating to use with digital sensors? cants work out why it doesnt seem to be working.
It only compiles, it does not even attempt to work until you up load it. You can compile any old rubbish as long as it is syntactical correct, it doesn't mean the logic is right.