Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

two way checking of array

Status
Not open for further replies.

varunme

Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,764
How can i make a c code for comparing an array in two ways ?

like a series of street light switch on and off when some vehicle or a pedestrian come infront of a sensor.
each light consist of two sensors , the light switch on when the pedstrian crosses one sensor and switch off when it crosses another but switch on another light,
The vehicle is coming from both sides so , i have to compare and check the array in two ways.
 

How can i make a c code for comparing an array in two ways ?

like a series of street light switch on and off when some vehicle or a pedestrian come infront of a sensor.
each light consist of two sensors , the light switch on when the pedstrian crosses one sensor and switch off when it crosses another but switch on another light,
The vehicle is coming from both sides so , i have to compare and check the array in two ways.

I understood the application, but didn't get the array comparison. How is this array involved with sensors? We are talking about C language array right? What the array's values are representing? Are those values steady (flash stored) or they are changing? Please be more descriptive about it.
 

yes , arrays are the numbers of the sensors in series and the series of the street lights , values 1, 1.1 , 2, 2.1, 3, 3.1 ............... , they are steady (yes flash stored with micro for demo purpose ) for actual implementation it will change , but now consider only the stored ones . program will be like this
Code:
for(i=1; i<=10; i++)
{ 
         sensor[i] =10;
         light[i] =10;
                  if (sensor[i] ==1;
                           {
                                  light[i] = 1;
                            }
                 else
                            {

                                 light[i] = 0;
                            }

}


the values inbetween like 1.1, 2.1, 3.1 etc are for switching off the lights
 

What are the types of the array?
How do you use this 1.2 or 2.2 value, is it a float ?
You already have two states 0 and 1 for the light, what do they represent and what is the need for a 1.1 value (instead of 2 or 3 for example)?

Alex
 

because for each light in there is a sensor for switch on the light that is "1" and for switch off that light and switch on the next one that is "1.1" ,
actually what I am trying to do is that ,
The lights are at 50% intensity all other time except when some pedestrian or a vehicle cuts the sensor ,
when some obstacle comes then it will go to 100% , all these are controlled by PWM. I don't know what to put inside the else statement
 

I don't see a reason for float values. This pseudocode you posted seems reasonable but without the use of the two statements sensor =10; and light =10; because this will force the else statement to be always executed. Since sensor is a digital (as implied) value read from the sensor, you shouldn't edit it manually and light on the other hand has to be 0 or 1 because it is a digital output. The value of 10 has no practical meaning for both arrays' values. As you described the application, both arrays should have exclusively 0 or 1 values. 10 is just the number of both sensors and outputs, you use it as a loop counter and has no further usage in this part of code.

With this coding way you have described, I don't see how those arrays could be stored in flash because they are continuously updated.
 
Last edited:
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
The number is stored in the array . means ,
PORTC.1 = first
PORTC.2 = second
PORTC.3=third
etc ,

can we implement it dynamically ?

PORTC. ?
 

The number is stored in the array . means ,
PORTC.1 = first
PORTC.2 = second
PORTC.3=third
etc ,

can we implement it dynamically ?

PORTC. ?


OK now I get it. You could do something like:

Code:
for(i=1; i<=10; i++)
{ 
  if (sensor[i] == 1)
  {
    PORTC |= (1<<i);
  }
  else
  {
    PORTC &= ~(1<<i);
  }
}

But you didn't specify the MCU you are using. If there are pins from C1 to C10, then this code is OK. If not, then other solutions must be implemented.
One more addition. You start the loop from 1, so sensor[0] is dummy, that means that you should declare the array as unsigned char sensor[11] and a non usable value will be stored in sensor[0]. Be carefull, arrays in C start counting from 0 and not 1. If you want to start from sensor[0], then instead of (1<<i) you should use (1<<(i+1)) and at the same time the loop must be implemented from i=0 to i<10 and not from i=1 to i<=10.
Or on the other hand you could leave the code as I posted it with a 10 elements array and just modify the if statement as if (sensor[i-1] == 1)
All this stands because you start from PORTC1 and not PORTC0.

Hope that this is what you are looking for.
 
Last edited:
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
About MCU , i didnt confirmed , i want to use the one with more number of hardware PWMs
have some doubts , i wasnt that sure with the bitwise operators , once i get clear it , i will post the doubts , thank you

---------- Post added at 05:15 ---------- Previous post was at 04:54 ----------

I have to check this both ways ( the vehicle or a pedestrian whose direction of movement ) , how can i implement that ? , loop inside loop ?
 
Last edited:

varunme said:
I have to check this both ways ( the vehicle or a pedestrian whose direction of movement ) , how can i implement that ? , loop inside loop ?
Can you please make clear why you separate the vehicle and the pedestrian case? If a sensor is "cut" the corresponding light will be on 100%. Else 50%. Is that right? How direction is involved? You read only sensors. If a pedestrian comes from right to left, the lights will follow the same direction too from sensor to sensor. If it is from left to right, the lights will then follow the inversed direction. Same stands for vehicles. Are there different sensors for pedestrians and vehicles?
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Can you please make clear why you separate the vehicle and the pedestrian case? If a sensor is "cut" the corresponding light will be on 100%. Else 50%. Is that right? How direction is involved? Are there different sensors for pedestrians and vehicles?


i used "and" , as literal , sorry

The loop is iterating only in forward direction , if its in opposite direction then how it will follow ?,
I am using the same sensor for both side of the road , that's why the direction comes into play.
 

varunme said:
I am using the same sensor for both side of the road , that's why the direction comes into play.

Maybe I cannot understand what you mean. Forget about programming, loops etc. Let's talk about the application. Let's say you have 10 sensors and 10 lights. Sensor 1 and light 1 is on the left side. Sensor 10 and light 10 on the right.
So a vehicle comes from the left side. Sensor 1 is "cut" and light 1 is on. Then it goes off sensor 1 and "cuts" sensor 2. So light 1 is now off (or 50% as you mentioned) and light 2 is on. This goes on until sensor 10. After vehicle passes from sensor 10, light 10 returns to 50%. So lights will be on from left to right in a case like that.
Then a pedestiran comes from right to left this time. Light 10, then 9, and then 8 will be on. At sensor 7 the pedestrian gets tired and stops. Light 7 will be at 100% until he/she starts walking again so the lights will be on from 7 to 1 as the pedestrian walks. When he/she leaves the sight of sensor 1, then light 1 also returns to 50%. In this case, lights will be on from right to left.
Is this the real scenario of the application as described? If not can you please describe it in details?
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
yes , exactly.

My doubts are if two vehicles from left to right and two from right to left , then our code works ?
For that purpose , our loop works ?
 

varunme said:
My doubts are if two vehicles from left to right and two from right to left , then our code works ?
Yes it works. Staying with the previous post example, if sensor 1 and 10 are "cut" at the same time, then lights 1 and 10 will be on at the same time as well. The only thing is that I talked about digital outputs (0 or 1), but you want pwm 50% or 100%. That's OK, it can be easilly adjusted. You don't have to worry about loop inside loop. But: how are you going to implement the pwm? Do C1 to C10 pins have PWM module, or you are going to implement a software PWM with the tick counts of a timer? Let's get this straight first and then finalize the code.
 
Last edited:
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
I am going to use only 5 , so C1 to C5 , I found a PIC with 5 PWMs , if otherwise the PIC is not available i will go for the software PWM.
 

varunme said:
I am going to use only 5 , so C1 to C5 , I found a PIC with 5 PWMs
So let's make an example with 5 lights then. If you need more, just edit the define line.
By the way it's useful to learn that you are using PIC, I will go with the example until PWM drive. I don't use PIC, so you must put the PWM code by yourself or start a new thread about it.

Code:
#define _TOTAL_LIGHTS  5

for(i=0; i<_TOTAL_LIGHTS; i++)
{ 
  if (sensor[i] == 1)
  {
    light[i] = 1;
  }
  else
  {
    light[i] = 0;
  }
}

Until now inside light there is the information if we need 50% or 100% PWM. When the part of code comes that you need to drive your PWMs, then you could handle them inside a loop.

Code:
for(i=0; i<_TOTAL_LIGHTS; i++)
{
  DriveLights(i);
}

void DriveLights (unsigned char thisLight)
{
  if (light[thisLight])
  {
    SetPWM(thisLight, 100);
  }
  else
  {
    SetPWM(thisLight, 50);
  }
}

Now as I said, I don't know the PWM code for PIC. In AVR for example, for the 8 bit PWM in a specific PWM mode, there is the OCR registers that store the duty cycle. OCR0A for one pin, OCR0B for another etc. And those registers have the value 127 for 50% and 255 for 100%. Let's assume random register names and 127-255 values.

Code:
void SetPWM (unsigned char thisPWM, unsigned char dutyCycle)
{
  volatile unsigned char dCycle;
  if (dutyCycle == 100)
    dCycle = 255;
  else if (dutyCycle == 50)
    dCycle = 127;

  switch (thisPWM)
  {
    case 0:
      A = dCycle;  //A is the register name you need for the C1 pin duty cycle
      break;
    case 1:
      B = dCycle;
      break;
    case 2:
      C = dCycle;
      break;
    case 3:
      D = dCycle;
      break;
    case 4:
      E = dCycle;  //E is the register name you need for the C5 pin duty cycle
      break;
    default:
      break;
  }
}

If you like to set the duty cycles inside a loop and not with cases, you will need an array of flash pointers to hardware registers A to E (compiler depended syntax), or an array of function pointers. I would suggest to stay away from this for the time being. Once you have a working code, then you could experiment with the higher level of programming.
 
Last edited:
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Can i write as ?

#define _TOTAL_LIGHTS 5

for(i=0; i<_TOTAL_LIGHTS; i++)
{
if (PORTD. == 1)
{
PORTC. = 1;
}
else
{
PORTC. = 0;
}
}
 

Can i write as ?

#define _TOTAL_LIGHTS 5

for(i=0; i<_TOTAL_LIGHTS; i++)
{
if (PORTD. == 1)
{
PORTC. = 1;
}
else
{
PORTC. = 0;
}
}


Since you are you using pwm no, you can't.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top