Pages: [1]   Go Down
Author Topic: Running a loop once (Beginner talk!)  (Read 747 times)
0 Members and 1 Guest are viewing this topic.
Chonburi, Thailand
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm new and I am completely losing my mind with my uno! I've been interested in open source hardware since building a monome a year or so ago.

I've basically gone from knowing next to no programming to finally wrapping my head around it (albeit slowly!) and it's making me pretty happy on the inside! yay!

I am wanting to run a loop once. Basically all I need is for my lightPin to sense brightness and once it senses, the motor will power on (HIGH) and delay 2000ms till (LOW) but no matter what combination of statements I use, I can only manage to have it turn on and stay on constantly until my lightPin senses darkness. I've pasted my code below.

I hope this makes sense. The project I am attempting is to simply open up a set of household blinds once ambient light has hit a predetermined level using "threshold". Obviously once ambient light falls below a certain level, I will add to the code so it closes. I'll figure that out later.

Quote
int lightPin = 0;
int motorPin = 9;

void setup()
{
 pinMode(motorPin, OUTPUT);
 pinMode(lightPin, INPUT);
}

void loop() {
   if (analogRead(lightPin) > 300) {     
      digitalWrite(motorPin, HIGH);
      delay(2000);
      digitalWrite(motorPin, LOW);
        }
   else if (analogRead(lightPin) < 300) {
      digitalWrite(motorPin, LOW);
        }
  }



Thanks heaps guys and maybe it's a good idea to have an "Absolute Beginner Talk" forum so people like me don't flood the "smart people" forum with stupid questions! smiley
Logged

Z
Australia
Offline Offline
Newbie
*
Karma: 2
Posts: 15
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

could do something like:

Code:
int lightPin = 0;
int motorPin = 9;
boolean trigger=false;

void setup()
{
 pinMode(motorPin, OUTPUT);
 pinMode(lightPin, INPUT);
}

void loop() {
   if (analogRead(lightPin) > 300 && trigger==false) {     
      digitalWrite(motorPin, HIGH);
      delay(2000);
      digitalWrite(motorPin, LOW);
      trigger=true;
        }
   if (analogRead(lightPin) < 300) {
      digitalWrite(motorPin, LOW);
      trigger=false;
        }
  }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46063
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and delay 2000ms till (LOW)
This requirement does not make sense. If the motor is supposed to run for 2 seconds, that's one requirement. If the motor is supposed to run until the sensor goes LOW, that is a different requirement.

Perhaps your issue with the code is really a matter of not being able to clearly define what the code is supposed to do.

Would you care to try again?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Prototype Limited Edition owner! lol
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int lightPin = 0;
int motorPin = 9;
boolean trigger=false;

void setup()
{
 pinMode(motorPin, OUTPUT);
 pinMode(lightPin, INPUT);
}

void loop() {
   if (analogRead(lightPin) > 300 && !trigger) {    
      digitalWrite(motorPin, HIGH);
      delay(2000);
      digitalWrite(motorPin, LOW);
      trigger=true;
        }
   if (analogRead(lightPin) < 300) {
      digitalWrite(motorPin, LOW);
      trigger=false;
        }
  }

 smiley-lol  smiley-wink
I would do !trigger, much cleaner.
Edit: hm no bold tags in quote...
« Last Edit: January 26, 2011, 07:14:44 am by Intosia » Logged

Chonburi, Thailand
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int lightPin = 0;
int motorPin = 9;
boolean trigger=false;

void setup()
{
 pinMode(motorPin, OUTPUT);
 pinMode(lightPin, INPUT);
}

void loop() {
   if (analogRead(lightPin) > 300 && !trigger) {    
      digitalWrite(motorPin, HIGH);
      delay(2000);
      digitalWrite(motorPin, LOW);
      trigger=true;
        }
   if (analogRead(lightPin) < 300) {
      digitalWrite(motorPin, LOW);
      trigger=false;
        }
  }

 smiley-lol  smiley-wink
I would do !trigger, much cleaner.
Edit: hm no bold tags in quote...

Worked a charm and thanks for going to the effort! After reading through the code carefully, it makes perfect sense! This community rules. smiley-grin
Logged

Pages: [1]   Go Up
Jump to: