Go Down

Topic: Coding for two if statement with on digitalread (Read 3 times) previous topic - next topic

ddyxpy

Hey, guys, I am currently doing a school project which asks me to program a vehicle with ARDUINO UNO board. They asked us to start the motor at the first black line and stop at the second black line, I bought a sensor which can detect blackline, but I have no experience about computing, can anyone please tell me about how to program this? Thank you

PeterH

If you were controlling it manually using only the the information provided by those sensors, how would you go about doing it?
I only provide help via the forum - please do not contact me for private consultancy.

JimboZA


If you were controlling it manually using only the the information provided by those sensors, how would you go about doing it?


And while you're thinking about that, which is exactly the right approach (figure out the process before the program), have a look at the Tutorial Page and get the basics of programming under your belt. Also have a look at whatever documentation is available for the sensor you bought, and figure out how to hook it up, and read its values.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

ddyxpy

#3
Oct 05, 2013, 10:48 am Last Edit: Oct 06, 2013, 05:47 am by CrossRoads Reason: 1
Hi, guys, I am doing a school project which required us to make a car start at one black line and stop at another black line. I bought a sensor and I also tried if statement, but it doesn't seems to work since it can only work for one digitalWrite. can somebody please help me about what kind of code should I use and how should I program it please? Thank you

[Topics merged  - moderator]

JimboZA

Well, firstly, sensors by definition read, not write....

But second and maybe more important, did you try my suggestion in your other, almost identical post, which was to:

Quote
have a look at the Tutorial Page and get the basics of programming under your belt. Also have a look at whatever documentation is available for the sensor you bought, and figure out how to hook it up, and read its values.


And lastly, you're likely to get moderated for this second thread. Re-posting won't help you get more info, it just pisses people off. If you tried what was suggested to you by me and by PeterH, respond in the other thread. If you don't like our suggestion, well, that is of course your choice....

In the first thread, at least help us to help you by giving details of the sensor, a schematic of the circuit, and post your code so far. Tell us what did and didn't happen, and what should have happened.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

ddyxpy

Sorry, I could not find my previous post thats why I post another one. I have looked the tutorial, but I can't really find the tut for which I want. Here is my code I got stuck there, because I dont know how to put the secodn if statement in. The sensor is a black and white sensor, it can read either 1024(non black colour) and 0 or 1( black colour). I also have a L239D chip which should be helpful. Can you please tell me how to put the second IF statement in?
int sensePin =0;//sensor
int mainswitch =9;// main switch of L239D
int L=3; //one leg of the L239D
int D=4; //the other leg of L239D

void setup(){
  analogReference(DEFAULT);
  pinMode(A0,INPUT);
  pinMode(mainswitch, OUTPUT);
  pinMode(L,OUTPUT);
  pinMode(D,OUTPUT);
}

void loop(){
  int val = analogRead(sensePin);
if(val<5) digitalWrite(mainswitch, HIGH);//give power to the motor
if(val<5) digitalWrite(L, HIGH);
if(val<5) digitalWrite(D, LOW);//combination make the motor turns clockwise
}[/move]

PaulS

Code: [Select]
if(val<5) digitalWrite(mainswitch, HIGH);//give power to the motor
if(val<5) digitalWrite(L, HIGH);
if(val<5) digitalWrite(D, LOW);//combination make the motor turns clockwise

Or:
Code: [Select]
if(val < 5)
{
  digitalWrite(mainswitch, HIGH);//give power to the motor
  digitalWrite(L, HIGH);
  digitalWrite(D, LOW);//combination make the motor turns clockwise
}


Which is clearer and easier to read?

The code you posted does something. You haven't said what. That may or may not be what you want. You haven't said.

Doing something else when the value is not less than 5 might, or might not, be important.

zoomkat

Some reading material that might have useful info.

https://www.google.com/search?as_q=line+follow+robot&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Fforum.arduino.cc%2Findex&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

ddyxpy

Yeah, the question is that the second black line have the same digitalread which is <5, and when I put the scond if statement which is "if (val<5) digitalWrite(mainswitch, LOW)" and the motor will not start. I guess these two If statement activate at the same time, but what I really want if to start the first IF and then start the second IF, can you please help me with that? Thankyou

PaulS

Quote
I guess these two If statement activate at the same time

They are evaluated at nearly the same time (there is some delay while the pins a turned on or off).

What you need is to look at the state change detection example, and consider how to adapt your code. What I mean is this: you see a black line (where the car starts). Then, you don't see the black line (the car has moved beyond the starting line). Then, you see a black line (the car reached the stop line). The transition from black to white is like a switch being released. The transition from white to black is like a switch being pressed.

You need to do something different depending on how many white to black or black to white transitions have occurred. Which implies that you need to detect them and to count them.

ddyxpy

Thank you for the helping so far. I have another problem which is that the sensor can detect the black line many times withnin a second and this enables arduino to perform the loop many times. So how can I make like a short time interval between the first and second IF statement like after the motor start, make the motor runs for 2 seconds then start detecting the second IF statement. I tried delay but it does not seems to work. here is my code
int sensePin = 0;
int mainswitch = 9;//switch of the motor
int L = 3;
int D = 4;//L and D controls the direction of rotation of motor

void setup(){
  analogReference(DEFAULT);
  pinMode(A0,INPUT);
  pinMode(mainswitch, OUTPUT);
  pinMode(L,OUTPUT);
  pinMode(D,OUTPUT);
}

void loop(){
  int val = analogRead(sensePin);
if(val<10) {digitalWrite(mainswitch, HIGH);
digitalWrite(L, HIGH);
digitalWrite(D, LOW);
delay(2000);

  if(val<20&& L==HIGH)
  digitalWrite(mainswitch,LOW);
}}

PaulS

Quote
I have another problem which is that the sensor can detect the black line many times withnin a second

But, it detects a transition from white to black (or black to white) just once. You really MUST detect transitions, not just current conditions.

ddyxpy

Hi, there, I have made a code which is based on the Arduino tut "State Changing Detection" but it does not seeems to work, can you please check where I went wrong please? Thanktou
const int sensePin = 0;
const int mainswitch = 9;//switch of the motor
const int L = 3;
const int D = 4;//L and D controls the direction of rotation of motor
int lastsensorstate = 0;
int sensorpushcounter= 0;//count the number of times that the sensor passed the black line
void setup(){
  analogReference(DEFAULT);
  pinMode(A0,INPUT);
  pinMode(mainswitch, OUTPUT);
  pinMode(L,OUTPUT);
  pinMode(D,OUTPUT);
}

void loop(){
  int sensorstate = digitalRead(sensePin);
  int val = analogRead(sensePin);
  if (sensorstate != lastsensorstate) {//checking if the current sensor state equal to the last sensor state
    if (sensorstate<30) {
      sensorpushcounter++;// if the sensorstate becoms <30 count the number of time which it changes
     
    }
  }
  if (sensorpushcounter % 1 ==1){//when it changes once start motor
    digitalWrite(mainswitch,HIGH);
    digitalWrite(L, HIGH);
    digitalWrite(D, LOW);
  }
  if (sensorpushcounter%2 ==1){//when it changes twice stop the motor
      if(val<20&& L==HIGH)
    digitalWrite(mainswitch, LOW);
  }}

   

PaulS

Quote
but it does not seeems to work,

The code does something. Send me the robot, so I can see what ti does. Or, explain what it does.

You expect it to do something. Send me your brain so I can see what that is. Or, explain what it is you expect.

Code: [Select]
  pinMode(A0,INPUT);
const int sensePin = 0;
int sensorstate = digitalRead(sensePin);
  int val = analogRead(sensePin);

What pin are you trying to use, for what purpose? You are setting the mode of the first analog pin as a digital pin. But, then, you are reading from the first digital pin as a digital pin, and reading from the first analog pin as an analog pin.

Make up your mind which pin you are using, and whether it is being used as a digital pin or an analog pin.

ddyxpy

I am designing a car which can start at when it detects the first black line and stop at the second black line. So what I expected is that the counter can count the times which the sensor moved from one colour to black colour( there are significant values of reading between black colour and other colour). I want the motor to start when the counter counts one( first time detect the black line) and stop  when the counter counts two( second time detect the black line). For the pin, I connect the sensor to the analog reading parts, but I dont really understand how the counter works, I just follow the pin numbers on the tutortial( buttonstatechange). can you please help me to make it work? Thanks

Go Up