Pages: [1]   Go Down
Author Topic: variable  (Read 576 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi there i'm trying to make an LED blink on and off but the daley i want variable from 5min to 25min
this code seems to turn the LED on and stays on but if i reduce the numbers down from 300000 to 30000 it seems to turn the LED off after 20seconds
any ideas what going on?

Code:

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT); 
}

void loop() {
  // read the value from the sensor:
  int sensorValue = analogRead(sensorPin);
  int sensor = map(sensorValue, 0, 1023, 300000UL, 1500000UL); 
  // turn the ledPin on
  digitalWrite(ledPin, HIGH); 
  // stop the program for <sensorValue> milliseconds:
  delay(sensor);         
  // turn the ledPin off:       
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:
  delay(sensor);                 
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're using an int when you should be using a long
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok whick on?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31534
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sensor
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You may want to look at the blink without delay example to see how to improve responsiveness.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

am i on the right line?
i did noticed the LED starts LOW not HIGH?

Code:


const int ledPin =  13;      // the number of the LED pin
long sensorPin = A0;    // select the input pin for the potentiometer
long sensorValue = 0;  // variable to store the value coming from the sensor
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);     
}

void loop()
{

  long sensorValue = analogRead(sensorPin);
  long sensor = map(sensorValue, 0, 1023, 30000UL, 1500000UL);
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > sensor) {

    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
am i on the right line? 
It probably took you as long to post as it would have done to find out.

Quote
i did noticed the LED starts LOW not HIGH?
Is that a problem? You can change it if you like.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

will i have tried it lol
i tried changing the high and low around still not bring the LED on straight forward?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
still not bring the LED on straight forward?
I don't understand what you're saying.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Leeds, UK
Offline Offline
Edison Member
*
Karma: 72
Posts: 1642
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Set the LED pin to be high in the setup, just after you have set it to be an output.
Logged

~Tom~

Pages: [1]   Go Up
Jump to: