LED code 16 relay - Code help

I was watching on youtube How to make Automatic Stairs Light Control with Arduino | Automatic Stairs Light Control - YouTube and I did everything like him.

I uploaded the same code and didn't change anything except "Distance for human read" it was 8 and I changed it to 15.

My reley is turning off and on automatically , without sensor detection... My friend told me to change something in code for "LOW TO HIGH" something like that , but I didn't understand him :slight_smile:

This is the code... can someone tell what should I do :slight_smile:

Thanks.

This is the code

int set_dis = 8;    // Set distance for human read
int set_step = 12;   // Set step for Stairs Limt
int set_shift = 500; // Set shifting time for step to step MilliSecond
int set_timer = 20;   // Set on stay time for light on Second

#define e_s1 A0 //echo pin
#define t_s1 A1 //Trigger pin

#define e_s2 A2 //echo pin
#define t_s2 A3 //Trigger pin

long dis_a=0,dis_b=0;
int flag1=0, flag2=0;
int i=0;
int Second = 0;
word MilliSecond = 0; 
bool timerStart = false;

int OutPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};


//**********************ultra_read****************************
void ultra_read(int pin_t,int pin_e,long &ultra_time){
long time;
digitalWrite(pin_t,LOW);
delayMicroseconds(2);
digitalWrite(pin_t,HIGH);
delayMicroseconds(10);
time=pulseIn (pin_e,HIGH);
ultra_time =  time / 29 / 2; 
}


void setup(){
Serial.begin(9600);// initialize serial communication at 9600 bits per second:

for (i=0; i<set_step; i++) {
    pinMode(OutPins [i], OUTPUT);
}

pinMode(e_s1,INPUT);
pinMode(t_s1,OUTPUT);

pinMode(e_s2,INPUT);
pinMode(t_s2,OUTPUT);

 noInterrupts();         // disable all interrupts
 TCCR1A = 0;             // set entire TCCR1A register to 0  //set timer1 interrupt at 1kHz  // 1 ms
 TCCR1B = 0;             // same for TCCR1B
 TCNT1  = 0;             // set timer count for 1khz increments
 OCR1A = 1999;           // = (16*10^6) / (1000*8) - 1
 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
 // turn on CTC mode
 TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
 TCCR1B |= (1 << CS11);  // enable timer compare interrupt
 TIMSK1 |= (1 << OCIE1A);
 interrupts();           // enable
 
delay(1000); // Waiting for a while
}


void loop(){ 
data();
  
if(Second==0){
if(flag1==1){ flag1=0; timerStart = false;
delay(set_shift);
for(i=set_step-1; i>-1; i--){
digitalWrite(OutPins [i], LOW);
data();
out1();
delay(set_shift);
  }
 }
}

if(Second==0){
if(flag2==1){ flag2=0; timerStart = false;
delay(set_shift);
for(i=0; i<set_step; i++){
digitalWrite(OutPins [i], LOW);
data();
out2();
delay(set_shift);
  }
 }
}

}


void data(){
ultra_read(t_s1,e_s1,dis_b);
ultra_read(t_s2,e_s2,dis_a);

Serial.print("da:");Serial.println(dis_a);
Serial.print("db:");Serial.println(dis_b);  
Serial.print("time:");Serial.println(Second); 

if(dis_a<set_dis){
Second = set_timer;
delay(set_shift);
if(flag1==0 && flag2==0){ flag1=1;
for(i=set_step-1; i>-1; i--){
digitalWrite(OutPins [i], HIGH);
delay(set_shift);
}
timerStart = true;
}
}

if(dis_b<set_dis){
Second = set_timer;
delay(set_shift);
if(flag1==0 && flag2==0){ flag2=1;
for(i=0; i<set_step; i++){
digitalWrite(OutPins [i], HIGH);
delay(set_shift);
}
timerStart = true;
  }
 }
}

void out1(){
if(flag1==1 || flag2==1){i=0;}
}

void out2(){
if(flag1==1 || flag2==1){i=set_step-1;}
}


ISR(TIMER1_COMPA_vect){   
if(timerStart == true){MilliSecond++;
    if(MilliSecond >= 1000){MilliSecond = 0;
       Second--;
    }
  }  
}

Please read the instructions for posting and correct how you have shown your code.

Done! Thank you. :+1:

Don't forget the part about auto formatting. The way it is, it's painful to confirm the code block extents. While you're there, break any lines that contain more than one statement, into multiple lines, one statement per line (unless it's only a few characters). You have a lot of redundant {}.

It's surprisingly difficult to go back to understanding personalized formatting, once you've gotten used to more or less industry standard.

Also, did you try running the unmodified code from the link? If it doesn't work, then you may have a hardware problem.

A very important hint. When trying something for the first time get it to work before making any changes. That way when it works we know the software and hardware are good. That then narrows it down to what was changed. You might start again with the original code. When he wired his relays were they active high or low?

Is it possible to download code from already one working board , because I have working code that I need but its on the another board and its not secured.

What ?

Is it possible to check (see) current code (already uploaded) from another arduino board, so I can take that code because its working there is no errors.

Hello
Yes, the WWW offers a lot of solutions to do this.

could you give me one solution I was searching on WWW but I only found options for HEX code something like that..

Sounds about right. :+1:

.

.

So - are you going to properly post your code in your OP (Original Post)? :face_with_raised_eyebrow:

I found the code very hard to follow so I re-wrote it in my own style.

const unsigned MAX_SENSE_DISTANCE_CM = 50; // Max distance for human read
const int STEP_COUNT = 12; // Set step for Stairs Limt
const int MILLIS_BETWEEN_STEPS = 500; // Set shifting time for step to step MilliSecond
const int ON_TIME_MILLIS = 20000; // Set on stay time for light

const byte TopEchoPin = A0;
const byte TopTriggerPin = A1;

const byte BottomEchoPin = A2;
const byte BottomTriggerPin = A3;

bool StairsAreLit = false;
bool LitFromBottom;
unsigned long OnTimerStart;

// The stair output pins, bottom to top
int OutPins[STEP_COUNT] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

bool PersonPresent(int triggerPin, int echoPin)
{
  delay(30); // Let the echoes from the last ping die down
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  unsigned long pulseDuration = pulseIn (echoPin, HIGH, 30000);

  // Round trip time is 58.3 microseconds per cm
  unsigned distance_cm = pulseDuration / 58.3;
  
  return (distance_cm > 0 && distance_cm < MAX_SENSE_DISTANCE_CM);
}

void ChangeFromBottom(int state)
{
  for (int i = 0; i < STEP_COUNT; i++)
  {
    digitalWrite(OutPins[i], state);
    delay(MILLIS_BETWEEN_STEPS);
  }
}

void OnFromBottom()
{
  ChangeFromBottom(HIGH);
  StairsAreLit = true;
  LitFromBottom = true;
  OnTimerStart = millis();
}

void OffFromBottom()
{
  ChangeFromBottom(LOW);
  StairsAreLit = false;
}

void ChangeFromTop(int state)
{
  for (int i = STEP_COUNT - 1; i >= 0; i--)
  {
    digitalWrite(OutPins[i], state);
    delay(MILLIS_BETWEEN_STEPS);
  }
}

void OnFromTop()
{
  ChangeFromTop(HIGH);
  StairsAreLit = true;
  LitFromBottom = false;
  OnTimerStart = millis();
}

void OffFromTop()
{
  ChangeFromTop(LOW);
  StairsAreLit = false;
}

void setup()
{
  Serial.begin(9600);// initialize serial communication at 9600 bits per second:

  for (int i = 0; i < STEP_COUNT; i++)
  {
    pinMode(OutPins [i], OUTPUT);
  }

  pinMode(TopEchoPin, INPUT);
  pinMode(TopTriggerPin, OUTPUT);

  pinMode(BottomEchoPin, INPUT);
  pinMode(BottomTriggerPin, OUTPUT);
}

void loop()
{
  // Have the stairs been lit long enough?
  if (StairsAreLit && millis() - OnTimerStart >= ON_TIME_MILLIS)
  {
    // The stairs have been lit for ON_TIME_MILLIS
    // Time to turn them off.
    if (LitFromBottom)
      OffFromBottom();
    else
      OffFromTop();
  }

  // Is there someone at the top of the stairs?
  if (PersonPresent(TopTriggerPin, TopEchoPin))
  {
    // Someone is at the top of the stairs

    if (!StairsAreLit)
    {
      // Light up the staris from the top
      OnFromTop();
    }
    else
    {
      // Stairs are already lit.  If this is someone
      // from the same direction, extend the time
      if (!LitFromBottom)
      {
        // Already detected this person, just extend the time
        OnTimerStart = millis();
      }
    }
  }

  // Is there someone at the bottom of the stairs?
  if (PersonPresent(BottomTriggerPin, BottomEchoPin))
  {
    // Someone is at the bottom of the stairs
    if (!StairsAreLit)
    {
      // Light up the stairs from the bottom
      OnFromBottom();
    }
    else
    {
      // Stairs are already lit.  If this is someone
      // from the same direction, extend the time
      if (LitFromBottom)
      {
        // Already detected this person, just extend the time
        OnTimerStart = millis();
      }
    }
  }
}

I will try to do something , change some codes... high to low, low to high... thank you

I noticed that in the video the stairs would start re-lighting if someone arrived before the steps were all off. I re-designed the sketch as a state machine to allow for this behavior.

const unsigned MAX_SENSE_DISTANCE_CM = 50; // Max distance for human read
const int STEP_COUNT = 12; // Set step for Stairs Limt
const int MILLIS_BETWEEN_STEPS = 500; // Set shifting time for step to step MilliSecond
const int ON_TIME_MILLIS = 20000; // Set on stay time for light

const byte TopEchoPin = A0;
const byte TopTriggerPin = A1;

const byte BottomEchoPin = A2;
const byte BottomTriggerPin = A3;

// The stair output pins, bottom to top
int OutPins[STEP_COUNT] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

enum States {IDLE,
             TURNING_ON_UP, ALL_ON_UP, TURNING_OFF_UP,
             TURNING_ON_DOWN, ALL_ON_DOWN, TURNING_OFF_DOWN
            } State = IDLE;

bool PersonPresent(int triggerPin, int echoPin)
{
  delay(30); // Let the echoes from the last ping die down
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  unsigned long pulseDuration = pulseIn (echoPin, HIGH, 30000);

  // Round trip time is 58.3 microseconds per cm
  unsigned distance_cm = pulseDuration / 58.3;

  return (distance_cm > 0 && distance_cm < MAX_SENSE_DISTANCE_CM);
}

void setup()
{
  Serial.begin(9600);// initialize serial communication at 9600 bits per second:

  for (int i = 0; i < STEP_COUNT; i++)
  {
    pinMode(OutPins [i], OUTPUT);
  }

  pinMode(TopEchoPin, INPUT);
  pinMode(TopTriggerPin, OUTPUT);

  pinMode(BottomEchoPin, INPUT);
  pinMode(BottomTriggerPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  static unsigned long StateChangeTime;
  bool atTop = PersonPresent(TopTriggerPin, TopEchoPin);
  bool atBottom = PersonPresent(TopTriggerPin, TopEchoPin);
  int stair;

  switch (State)
  {
    case IDLE:
      if (atTop)
      {
        State = TURNING_ON_DOWN;
        StateChangeTime = currentMillis;
      }

      if (atBottom)
      {
        State = TURNING_ON_UP;
        StateChangeTime = currentMillis;
      }
      break;

    case TURNING_ON_UP:
      // Light up from bottom to top
      stair = (currentMillis - StateChangeTime) / MILLIS_BETWEEN_STEPS;
      if (stair < STEP_COUNT)
        digitalWrite(OutPins[stair], HIGH);
      else
      {
        State = ALL_ON_UP;
        StateChangeTime = currentMillis;
      }
      break;

    case ALL_ON_UP:
      // If someone is present at the bottom while
      // the lights are on, extend the timer
      if (atBottom)
        StateChangeTime = currentMillis; // Re-start the timer

      // If the lights have been on long enough, start turning off
      if (currentMillis - StateChangeTime >= ON_TIME_MILLIS)
      {
        State = TURNING_OFF_UP;
        StateChangeTime = currentMillis;
      }
      break;

    case TURNING_OFF_UP:
      stair = (currentMillis - StateChangeTime) / MILLIS_BETWEEN_STEPS;
      if (stair < STEP_COUNT)
        digitalWrite(OutPins[stair], LOW);
      else
      {
        State = IDLE;
      }

      // If someone appears at the top before we are done
      // turning off lights from the bottom up, start turning
      // them on from the top down.
      if (atTop)
      {
        State = TURNING_ON_DOWN;
        StateChangeTime = currentMillis;
      }
      break;

    case TURNING_ON_DOWN:
      // Light up from top to bottom
      stair = (currentMillis - StateChangeTime) / MILLIS_BETWEEN_STEPS;
      if (stair < STEP_COUNT)
        digitalWrite(OutPins[(STEP_COUNT - 1) - stair], HIGH);
      else
      {
        State = ALL_ON_UP;
        StateChangeTime = currentMillis;
      }
      break;

    case ALL_ON_DOWN:
      // If someone is present at the top while
      // the lights are on, extend the timer
      if (atTop)
        StateChangeTime = currentMillis; // Re-start the timer

      // If the lights have been on long enough, start turning off
      if (currentMillis - StateChangeTime >= ON_TIME_MILLIS)
      {
        State = TURNING_OFF_DOWN;
        StateChangeTime = currentMillis;
      }
      break;

    case TURNING_OFF_DOWN:
      stair = (currentMillis - StateChangeTime) / MILLIS_BETWEEN_STEPS;
      if (stair < STEP_COUNT)
        digitalWrite(OutPins[(STEP_COUNT - 1) - stair], LOW);
      else
      {
        State = IDLE;
      }

      // If someone appears at the top before we are done
      // turning off lights, start turning them on
      if (atTop)
      {
        State = TURNING_ON_DOWN;
        StateChangeTime = currentMillis;
      }
      break;
  }
}

I tried your code , but sensors are not activating when moving my hand.

They are not motion sensors. They are distance sensors. Have you tested your sensors with a small sketch? If you always get 0 there is probably a mistake in your wiring.

const byte EchoPin = A0;
const byte TriggerPin = A1;

void setup()
{
  Serial.begin(115200);

  pinMode(TriggerPin, OUTPUT);
  pinMode(EchoPin, INPUT);
}

void loop()
{
  delay(30); // Let the echoes from the last ping die down
  digitalWrite(TriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TriggerPin, LOW);
  unsigned long pulseDuration = pulseIn (EchoPin, HIGH, 30000);

  // Round trip time is 58.3 microseconds per cm
  unsigned distance_cm = pulseDuration / 58.3;

  Serial.println(distance_cm);
}

yep, I have tested them and they are doing good. When I move my hand the "indicator" on arduino board turns ON"blinking"

how the program works for him and it doesn't work for me :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.