Go Down

Topic: Using a Photocell threshold to start a sequence (Read 53 times) previous topic - next topic

samuraismm

Hello,

I am new to Arduino usage and delved right in to help a friend with a project.  However I am having difficulty trying to get a sequence correct.

IDEA:  set of 2 LEDs will fade in and out constantly and when a light is introduced to a photocell a sequence of randomly placed LEDs will fade/blink.  Once light is removed from the sensor it reverts back to a constant glow from the original two LEDs

WHERE I STAND CURRENTLY: I have gotten the coding to have the LEDs fade in and out easily just by changing a few pin names and the delay.  I have managed to get the LEDs to stop when light is introduced to the system but I cannot figure out how to get it to start a new sequence once this happens.  What I have so far is below:



const int ledGaps = 3;
const int lightSensor = 0;
const int ledShapeA = 5;
const int ledShapeB = 2;
const int sensorThreshold = 200;

int fadeAmount = 5;
int sensorValue = 0;
int sensorMAX = 1023;
int sensorMIN = 0;
int glowBright = 0;
int shapeBright = 0;


void setup() {
  Serial.begin(9600);
  pinMode(ledGaps, OUTPUT);
  pinMode(ledShapeA, OUTPUT);
  pinMode(ledShapeB, OUTPUT);
  pinMode(lightSensor, INPUT);
}


void loop() {

  sensorValue = analogRead(lightSensor);
  Serial.print("Digital reading = ");
  Serial.println(sensorValue);
 
  {analogWrite(ledGaps, glowBright);

  glowBright = glowBright + fadeAmount;

  if (glowBright == 0 || glowBright == 255) {
    fadeAmount = -fadeAmount ;
  }
  delay(60);
  }
  if(sensorValue > sensorThreshold) {
    digitalWrite(ledShapeB, HIGH);
    delay(400);
    digitalWrite(ledShapeB, LOW);
    delay(500);
  }
}


Originally I tried to have the if() start a new sequence called sensed() but I couldn't figure out what I was missing to get that to work as it seems that the sensor slows down readings dramatically once light is introduced.  I'm not sure if this means that the program is running and I'm just missing something.  But it happens still once I tried the above wording of a basic blink directly into the statement.  I'm unsure if I'm missing a basic power supply issue of trying to run too many LEDs on one ground or if it's something else completely. The best news though is that it does revert back to the original fading pattern once light is removed so I am pretty sure I'm in the general ballpark?

Thanks a bunch people of internet-land!

PaulS

Quote
Originally I tried to have the if() start a new sequence called sensed() but I couldn't figure out what I was missing to get that to work as it seems that the sensor slows down readings dramatically once light is introduced.
I seriously doubt that the time it takes for the sensor to read the light level changes as the light level does. More likely, the time needed to take another reading was long because your function is blocking (probably using delay()). You need to post that code.

The name sensed() does not make sense. Good function names consist of a noun and a verb, like analogRead(), digitalWrite(), blinkLEDs(), etc.

samuraismm

Noted on the name bit will reword that.  But that is the code in it's entirety.


I was thinking it might be an issue with the delay().  I have the Serial Monitor up and it is noting that once I introduce light to the sensor that's above the threshold value of 200, it slows down a bit.  Regardless of this, it won't make the LED blink despite that is what is being written in.  Double checked my connections and it is connected to pin 2 and a digital ground.  I'm also noting that the original fading pattern slows as well once light is introduced.

samuraismm

const int ledGaps = 3;
const int lightSensor = 0;
const int ledShapeA = 5;
const int ledShapeB = 2;
const int sensorThreshold = 200;

int fadeAmount = 5;
int sensorValue = 0;
int sensorMAX = 1023;
int sensorMIN = 0;
int glowBright = 0;
int shapeBright = 0;

// the setup routine runs once when you press reset:
void setup() {
 Serial.begin(9600);
 pinMode(ledGaps, OUTPUT);
 pinMode(ledShapeA, OUTPUT);
 pinMode(ledShapeB, OUTPUT);
 pinMode(lightSensor, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {

 sensorValue = analogRead(lightSensor);
 Serial.print("Digital reading = ");
 Serial.println(sensorValue);

 {analogWrite(ledGaps, glowBright);

 glowBright = glowBright + fadeAmount;

 if (glowBright == 0 || glowBright == 255) {
   fadeAmount = -fadeAmount ;
 }
 delay(60);
 }
 if(sensorValue > sensorThreshold) {
  lightSensed();
 }
}

void lightSensed() {
   digitalWrite(ledShapeB, HIGH);
   delay(400);
   digitalWrite(ledShapeB, LOW);
   delay(500);
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy