Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Fading LED using a light sensor on: April 27, 2013, 09:28:11 am
Hello,


I have a light sensor, and, if it's dark outside, i want to turn ON an LED with a fading effect.
I have made the program but it doesn't wirok properly:

Code:
/*
  AnalogReadSerial
 Reads an analog input on pin 0, prints the result to the serial monitor.
 Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 
 This example code is in the public domain.
 */
const int led = 3;
// the setup routine runs once when you press reset:
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by



void setup() {
  pinMode (led, OUTPUT);   // sets pin 13 as digital output
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}


// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  if(sensorValue < 200 )
  {
   
    brightness = brightness + fadeAmount;
    delay(80);
    analogWrite(led, brightness);
  }
  else
  {
    analogWrite(led, 0);
    brightness = 0;
  }

  delay(10);        // delay in between reads for stability
}




If it's dark outside, the LED is turning ON with the fading effect but as soon as the LED hits 255 brightness, it starts all over again from 0 to 255 brightness.

How can I make the LED stay ON after his brighness goes from 0 to 255?
2  Using Arduino / Programming Questions / Re: Switch-case loop problem?! on: March 24, 2013, 11:04:09 am
It's working now, thank you very much Arrch

Code:
#include <Servo.h>



int var = 0;



int Lstate = 2;      // pin 2 - citire
int var_Lstate = 0; 

int pos_tm1 = 120;     // 127 best
int pos_tm2 = 30;    // variable to store the servo position
Servo myservo_tm;

int flag_tm1 = 0;
int flag_tm2 = 0;
long prag_steptm1 = 0;
long prag_steptm2 = 0;
long prag_tm1 = 0;
long prag_tm2 = 0;

long prag_pos = 0;

void setup()
{Serial.begin(9600);
pinMode (Lstate, INPUT);  // sets pin 2 as digital input

 myservo_tm.attach(5);
  myservo_tm.write(120);
}

void loop()
{
 
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
//  Serial.println(flag_tm2);
//  Serial.println(flag_tm1);
//  Serial.println(var_Lstate);
Serial.println(flag_tm1);
//Serial.println(pos_tm2);

   if(flag_tm1 == 1)
  {
    if(millis() - prag_steptm1 > 55)
    {
      prag_steptm1 = millis();
      if(pos_tm1 > 29)
      { 
        pos_tm1--;
        myservo_tm.write(pos_tm1);
      }
      else     
      flag_tm1 = 0; 
      }
    }
 
 
   if(flag_tm2 == 1)
  {
    if(millis() - prag_steptm2 > 55)
    {
      prag_steptm2 = millis();
      if(pos_tm2 < 121)
      { 
        pos_tm2++;
        myservo_tm.write(pos_tm2);
      }
      else
      flag_tm2 = 0;
    }
  }
 
 
 
  switch(var)
  {
  case 0:
     
      if(var_Lstate == 0)
    {   
     // pos_tm1 = 120;
     // pos_tm2 = 30;
      prag_tm1 = millis();
      prag_tm2 = millis();
      var = 1;
    }
  break;
 
 
  case 1:
      if( millis() - prag_tm1 > 3000)
    {
      flag_tm1 = 1;
    }
    if( millis() - prag_tm2 > 10000)
    {
      flag_tm2 = 1;   
      prag_pos = millis();
      var = 2;
    }
 
  break;
  case 2:
     if(millis() - prag_pos > 10)
     {
       if(flag_tm1 == 0 && flag_tm2 == 0)
       {
         pos_tm1 = 120;
         pos_tm2 = 30;
         var = 0;
       }
     }
  break;
  }
  }
 
 
 

I know I used different variables for all things i wanted to do, and i know i could use the same variable for more than one thing but, the comitee of examiners don't know so well all this programming stuff and i wanted to be easy for them to track all the commands using representative variable names.
I know i could use one case for every action i want but, in this way it seems more easier to track the program (for ex: case 2 does x,y and z actions).

My entire code for the bridge is almost ready.If some1 is interested i'll post it too.
Thank you all again for your help.

3  Using Arduino / Programming Questions / Re: Switch-case loop problem?! on: March 23, 2013, 05:50:42 am
@Arrch

Code:
#include <Servo.h>



int var = 0;



int Lstate = 2;      // pin 2 - citire
int var_Lstate = 0; 

int pos_tm1 = 120;     // 127 best
int pos_tm2 = 30;    // variable to store the servo position
Servo myservo_tm;

int flag_tm1 = 0;
int flag_tm2 = 0;
long prag_steptm1 = 0;
long prag_steptm2 = 0;
long prag_tm1 = 0;
long prag_tm2 = 0;


void setup()
{Serial.begin(9600);
pinMode (Lstate, INPUT);  // sets pin 2 as digital input

 myservo_tm.attach(5);
  myservo_tm.write(120);
}

void loop()
{
 
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
  //Serial.println(flag_tm2);
  //Serial.println(flag_tm1);
  //Serial.println(var_Lstate);
Serial.println(var);
   if(flag_tm1 == 1)
  {
    if(millis() - prag_steptm1 > 55)
    {
      prag_steptm1 = millis();
      if(pos_tm1 > 29)
      { 
        pos_tm1--;
        myservo_tm.write(pos_tm1);
      }
      else
      flag_tm1 = 0;
    }
   
  }
 
   if(flag_tm2 == 1)
  {
    if(millis() - prag_steptm2 > 55)
    {
      prag_steptm2 = millis();
      if(pos_tm2 < 121)
      { 
        pos_tm2++;
        myservo_tm.write(pos_tm2);
      }
      else
      flag_tm2 = 0;
    }
  }
 
 
  switch(var)
  {
  case 0:
      pos_tm1 = 120;
      pos_tm2 = 30;
      if(var_Lstate == 0)
    {   
     
      prag_tm1 = millis();
      prag_tm2 = millis();
      var = 1;
    }
  break;
 
 
  case 1:
      if( millis() - prag_tm1 > 3000)
    {
      flag_tm1 = 1;
    }
    if( millis() - prag_tm2 > 10000)
    {
      flag_tm2 = 1;
      var = 0;
    }
  break;
 
 
  }
}
 
 

Still doesn't work properly...
4  Using Arduino / Programming Questions / Re: Switch-case loop problem?! on: March 15, 2013, 03:43:02 pm
So where in program should i re-declare the value of pos_tm1 and pos_tm2 in order not to cause any other problems?
5  Using Arduino / Programming Questions / Re: Switch-case loop problem?! on: March 15, 2013, 03:30:54 pm
yeah, I saw that now too.

But I have a question:

Code:
pos = 120;


if(pos > 29)
      { 
        pos--;

After my logic this runs like this:
- if pos > 29 (it really is 120) it decrements the position until it will be 30 because 30>29 and 29=29

But the program in this case seems to decrement the position till 29.How is that possible?
6  Using Arduino / Programming Questions / Re: Switch-case loop problem?! on: March 15, 2013, 03:03:39 pm
Code:
#include <Servo.h>



int var = 0;



int Lstate = 2;      // pin 2 - citire
int var_Lstate = 0;  

int pos_tm1 = 120;     // 127 best
int pos_tm2 = 30;    // variable to store the servo position
Servo myservo_tm;

int flag_tm1 = 0;
int flag_tm2 = 0;
long prag_steptm1 = 0;
long prag_steptm2 = 0;
long prag_tm1 = 0;
long prag_tm2 = 0;


void setup()
{Serial.begin(9600);
pinMode (Lstate, INPUT);  // sets pin 2 as digital input

 myservo_tm.attach(5);
  myservo_tm.write(120);
}

void loop()
{
  
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
  //Serial.println(flag_tm2);
  //Serial.println(flag_tm1);
  //Serial.println(var_Lstate);
Serial.println(var);
   if(flag_tm1 == 1)
  {
    if(millis() - prag_steptm1 > 55)
    {
      prag_steptm1 = millis();
      if(pos_tm1 > 29)
      {  
        pos_tm1--;
        myservo_tm.write(pos_tm1);
      }
      else
      flag_tm1 = 0;
    }
    
  }
  
   if(flag_tm2 == 1)
  {
    if(millis() - prag_steptm2 > 55)
    {
      prag_steptm2 = millis();
      if(pos_tm2 < 121)
      {  
        pos_tm2++;
        myservo_tm.write(pos_tm2);
      }
      else
      flag_tm2 = 0;
    }
  }
  
  
  switch(var)
  {
  case 0:
      
      if(var_Lstate == 0)
    {  
      prag_tm1 = millis();
      prag_tm2 = millis();
      var = 1;
    }
  break;
  
  
  case 1:
      if( millis() - prag_tm1 > 3000)
    {
      flag_tm1 = 1;
    }
    if( millis() - prag_tm2 > 10000)
    {
      flag_tm2 = 1;
      var = 0;
    }
  break;
  
  
  }
}
  
  
    
    

Serial.println(var) - it starts in "0", after the sensor is triggered it goes to "1", it's doing what it should do with the motor, then it returns back in "0" state...then if the sensor is triggered again...it goes again in "1" but nothing happens [ the motor is not moving]..


LE: I checked and the flag is switching to "1" in the 2nd cycle[when the sensor detects movement again] but the motor doesn't move....
7  Using Arduino / Programming Questions / Re: Switch-case loop problem?! on: March 15, 2013, 02:46:16 pm
Code:
#include <Servo.h>



int var = 0;



int Lstate = 2;      // pin 2 - citire
int var_Lstate = 0; 

int pos_tm1 = 120;     // 127 best
int pos_tm2 = 30;    // variable to store the servo position
Servo myservo_tm;

int flag_tm1 = 0;
int flag_tm2 = 0;
long prag_steptm1 = 0;
long prag_steptm2 = 0;
long prag_tm1 = 0;
long prag_tm2 = 0;


void setup()
{Serial.begin(9600);
pinMode (Lstate, INPUT);  // sets pin 2 as digital input

 myservo_tm.attach(5);
  myservo_tm.write(120);
}

void loop()
{
 
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
  //Serial.println(flag_tm2);
  //Serial.println(flag_tm1);
  //Serial.println(var_Lstate);

   if(flag_tm1 == 1)
  {
    if(millis() - prag_steptm1 > 55)
    {
      prag_steptm1 = millis();
      if(pos_tm1 > 29)
      { 
        pos_tm1--;
        myservo_tm.write(pos_tm1);
      }
     // else
     // flag_tm1 = 0;
    }
   
  }
 
   if(flag_tm2 == 1)
  {
    if(millis() - prag_steptm2 > 55)
    {
      prag_steptm2 = millis();
      if(pos_tm2 < 121)
      { 
        pos_tm2++;
        myservo_tm.write(pos_tm2);
      }
      else
      flag_tm2 = 0;
    }
  }
 
 
  switch(var)
  {
  case 0:
     
      if(var_Lstate == 0)
    {   
      prag_tm1 = millis();
      prag_tm2 = millis();
      var = 1;
    }
  break;
 
 
  case 1:
      if( millis() - prag_tm1 > 3000)
    {
      flag_tm1 = 1;
    }
    if( millis() - prag_tm2 > 10000)
    {
      flag_tm2 = 1;
      var = 0;
    }
  break;
 
 
  }
}
 
 
   
   


I've checked with serial.print....the sensor reading and the 2 flags turn by turn...and they're doing just fine.... reading is 0 only when the sensor detected movement, and flags have the value "1" only when the motor is moving....after that, they're set back to "0".

So i still dont know why the cycle isn't repeated....
8  Using Arduino / Programming Questions / Re: Switch-case loop problem?! on: March 15, 2013, 02:08:00 pm
Code:
if(flag_tm1 == 1)
  {
    if(millis() - prag_steptm1 > 55)
    {
      prag_steptm1 = millis();
      if(pos_tm1 > 29)
      { 
        pos_tm1--;
        myservo_tm.write(pos_tm1);
      }
      else
      flag_tm1 = 0;
    }
   
  }

Still don't work smiley-sad
9  Using Arduino / Programming Questions / Re: Switch-case loop problem?! on: March 15, 2013, 01:50:12 pm
I set the flags 0 in case 0: before IF statement and no luck, still doesn't go all over again...
I set the flags 0 even in case 1: in the 2nd IF right before "var= 0" and still doesn't go all over again..
10  Using Arduino / Programming Questions / Switch-case loop problem?! on: March 15, 2013, 01:19:32 pm
The following code does:


0) wait for a movement to trigger the sensor
1) wait 5 seconds and lift the tollbooth
2) wait another 5 seconds and lower the tollbooth
*3) go in the first case and be ready for another cycle (detects movement then do those up/down motor steps)

It's only one problem( at 3rd point) -> the cycle doesn't repeat after the 2nd detection of movement, why is that?

Code:
#include <Servo.h>



int var = 0;



int Lstate = 2;      // pin 2 - reading the proximity sensor
int var_Lstate = 0; 

int pos_tm1 = 120;     // 127 best
int pos_tm2 = 30;    // variable to store the servo position
Servo myservo_tm;

int flag_tm1 = 0;
int flag_tm2 = 0;
long prag_steptm1 = 0;
long prag_steptm2 = 0;
long prag_tm1 = 0;
long prag_tm2 = 0;


void setup()
{
pinMode (Lstate, INPUT);  // sets pin 2 as digital input

 myservo_tm.attach(5);
  myservo_tm.write(120);
}

void loop()
{
 
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
 
 
   if(flag_tm1 == 1)
  {
    if(millis() - prag_steptm1 > 55)
    {
      prag_steptm1 = millis();
      if(pos_tm1 > 29)
      { 
        pos_tm1--;
        myservo_tm.write(pos_tm1);

      }
    }
  }
 
   if(flag_tm2 == 1)
  {
    if(millis() - prag_steptm2 > 50)
    {
      prag_steptm2 = millis();
      if(pos_tm2 < 121)
      { 
        pos_tm2++;
        myservo_tm.write(pos_tm2);

      }
    }
  }
 
 
  switch(var)
  {
  case 0:
      if(var_Lstate == 0)
    {
      prag_tm1 = millis();
      prag_tm2 = millis();
      var = 1;
    }
  break;
 
 
  case 1:
      if( millis() - prag_tm1 > 5000)
    {
      flag_tm1 = 1;
    }
    if( millis() - prag_tm2 > 10000)
    {
      flag_tm2 = 1;
      var = 0;
    }
  break;
 
 
  }
}
 
 
   
   
11  Using Arduino / Programming Questions / Re: Counter info on: March 13, 2013, 11:06:44 am
It's working perfectly now.
Thank you very much

Code:
if(millis() - lastStateChangeTime > 700)
    {
      lastStateChangeTime = millis();           // save the last time you blinked the LED
      if (ledState13 == LOW)                    // if the LED is off turn it on and vice-versa:
      {
        ledState13 = HIGH;
        ledState12 = LOW;
        tone(8, 2200, 250);
      }
      else
      {
        ledState13 = LOW;
        ledState12 = HIGH;
        tone(8, 1800, 250);
      }

      digitalWrite(ledPin13, ledState13);      // set the LED with the ledState of the variable:
      digitalWrite(ledPin12, ledState12);
    }
 
12  Using Arduino / Programming Questions / Re: Stepper Servo Motor help on: March 13, 2013, 11:04:00 am
It's working now, thank you for the hint.

Code:
if(millis() - lastStateChangeTime > 60)
  {
    lastStateChangeTime = millis();
    if(pos < 81)
    { 
      pos++;
      myservo.write(pos);
     
    }
  }
13  Using Arduino / Programming Questions / Stepper Servo Motor help on: March 13, 2013, 10:01:35 am
The code for a stepper servo-motor if i want to move it in one way only:

Code:
int pos = 20;    // the motor is at 20 degrees

void loop()
{
 while(pos < 81)    //while the position of the servomotor is lower than 81 degrees
 {
   pos++;      // it goes till 80 degrees
   myservo.write(pos);
   delay(60);         // the delay between steps - 60 ms
 }
}


My question is, how can I replace the delay() with millis()?
I've tryed:

Code:
long interval_B1 = 60;
int pos = 20;    // the motor is at 20 degrees

void loop()
{
  unsigned long currentTime_B1 = millis();

  if(currentTime_B1 - previousTime_B1 > interval_B1)
  {
    previousTime_B1 = currentTime_B1;
    while(pos < 81)
    {
      pos++;
      myservo.write(pos);
    }
  }
}

But it's not working properly, any hints?
14  Using Arduino / Programming Questions / Re: Counter info on: March 12, 2013, 06:42:08 am
Ok, so I tryed to make the first 2 steps:

1st step:
- if the left sensor (placed on the left of the bridge at a distance let's say...20cm) detects movement (that means a cargo-boat is aproaching the bridge)
- then turn ON the left marker LED (LED_L - left sensor) [ and leave it ON]

2nd step:
- then wait 5 seconds, start the audio ( one mini-speaker set at a specific frequency) and visual warnings on the bridge (2 red LEDs blinking) 

but it doesn't work fine and I got a question:

Code:
int var = 0;

// TOLLBOOTH
int ledState13 = LOW;           
int ledState12 = LOW;
const int ledPin13 =  13;     
const int ledPin12 =  12;
long previousTime_IT = 0;
long interval_IT = 700;   // intervat between flashing LED's inside the tollbooth warning system

// SHARP SENSOR
const int LED_L = 10;
const int LED_R = 11;
const int Lstate = 2;      // pin 2 - citire
const int Rstate = 4;      // pin 4 - citire
int var_Lstate = 0;       // reset to zero the variable used to read the state of the OUT pin of the sensor
int var_Rstate = 0;
int read_LED_L = 0;
int read_LED_R = 0;
long previousTime_WT = 0;
long interval_WT = 2000;   // interval between after reading the sensor's LED and activating the tollbooth
unsigned long currentTime_WT;


void setup() {               
  // TOLLBOOTH
  pinMode(ledPin13, OUTPUT);     
  pinMode(ledPin12, OUTPUT); 
 
  // SHARP SENSOR
  pinMode (LED_L, OUTPUT);   // led stanga
  pinMode (LED_R, OUTPUT);   // led dreapta
  pinMode (Lstate, INPUT);  // sets pin 2 as digital input
  pinMode (Rstate, INPUT);
 
 
}

// the loop routine runs over and over again forever:
void loop()
{
  var_Lstate = digitalRead(Lstate);    // reads the status of the sensor
  read_LED_L = digitalRead(10);

  switch(var)
  {
   case 0:
    if(var_Lstate == 0)
      digitalWrite (LED_L, HIGH); 
    if(read_LED_L == HIGH)
    {
        currentTime_WT = millis();
        var = 1;
    }   
   break; 

   case 1:
    if(currentTime_WT - previousTime_WT > interval_WT)
                {
                        previousTime_WT = currentTime_WT;
                        tollbooth();       
                }   
    break;     
  } 


}



void tollbooth()
{
  unsigned long currentTime_IT = millis();      // check to see if it's time to blink the LED; that is, if the
                                               // difference between the current time and last time you blinked
                                               // the LED is bigger than the interval at which you want to
                                               // blink the LED.
 
  if(currentTime_IT - previousTime_IT > interval_IT)
  {
    previousTime_IT = currentTime_IT;           // save the last time you blinked the LED
    if (ledState13 == LOW)                    // if the LED is off turn it on and vice-versa:
    {
      ledState13 = HIGH;
      ledState12 = LOW;
      tone(8, 2200, 250);
    }
    else
    {
      ledState13 = LOW;
      ledState12 = HIGH;
      tone(8, 1800, 250);
    }
   
    digitalWrite(ledPin13, ledState13);      // set the LED with the ledState of the variable:
    digitalWrite(ledPin12, ledState12);
  }
}


Maybe it doesn't work fine because of using a millie inside another millie?
15  Using Arduino / Programming Questions / Re: Counter info on: March 07, 2013, 08:54:12 am
Okay, thank you.

I will start all over again and use SWITCH-CASE statement.

I'll come back after this weekend with the code and if I'll stumble upon other problems.
Pages: [1] 2