Hi there. I have a school project, where, I will measure the speed of a car by:
Using photosensors. What I want my program to do is that, when I put my hand over the sensor, the program starts a counter. When I put my hand on the other sensor, it stops the counter, divides the time between my hand covering both sensors by a pre-set distance, and gets me a speed, which it inputs to the serial port. However, I get nothing on the serial port.
My connections are right, I'm sure, and I think its something to do with my counter coding. Any help, please?
int photoPin = A0;
int photoPin2 = A1;
int photo = HIGH;
int photo2 = HIGH;
unsigned long startMillis;
unsigned long currentMillis;
int LED1 = 3;
int LED2 = 4;
int LED3 = 6;
int LED4 = 7;
int MS;
int KMH;
int distance = 1;
void setup() {
pinMode(photoPin, INPUT);
pinMode(photoPin2, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
}
void loop() {
photo = analogRead(photoPin);
photo2 = analogRead(photoPin2);
if (photo >= 500) {
startMillis = millis();
}
if (photo2 >= 500) {
startMillis = currentMillis;
}
MS = currentMillis / distance;
KMH = MS * 3.6;
Serial.print(KMH);
if (KMH >= 50) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
}
I know my work is messy, with some probably very silly mistakes, but this is my second-ever code, so please bear with me.
Thanks!
Where do you set the value of currentMillis
UKHeliBob:
Where do you set the value of currentMillis
In the 'unsigned long'. I'm am sure I've done it wrong there
No, that just describes the type of currentMillis. It doesn't assign any value to it.
Assigning values nearly always involves being on the LH side of an = sign. Like in startMillis = millis().
Steve
Ok, guys. Deadline is nearing and my millis library isn't working. My teacher says that I can use a normal counter (like add one plus one, delay, add again) to make the project. I edited the code a little bit. Any help now? What i want it to do is to add a counter which is like counting seconds. Once again, I'm a newbie so my mistakes/questions might be silly.
int photoPin = A0;
int photoPin2 = A1;
int photo = HIGH;
int photo2 = HIGH;
int LED1 = 3;
int LED2 = 4;
int LED3 = 6;
int LED4 = 7;
int MS;
int KMH;
int distance = 1;
int counter;
int timeValue;
void setup() {
pinMode(photoPin, INPUT);
pinMode(photoPin2, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
Serial.begin(9600);
}
void loop() {
counter=0;
photo = analogRead(photoPin);
photo2 = analogRead(photoPin2);
Serial.println(photo);
Serial.println(photo2);
delay(1000);
if (photo >= 500); {
(counter=counter+1);
delay(1000);
Serial.println(counter);
if (photo2 >= 500) {
counter=timeValue;
Serial.println(timeValue);
}
}
MS = timeValue / distance;
KMH = MS * 3.6;
Serial.print(KMH);
if (KMH >= 50) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
}
if (photo >= 500);
You don't want a semicolon there
CtrlAltElite:
if (photo >= 500);
You don't want a semicolon there
Did that. Now, my printed counter only displays 1
CtrlAltElite:
Is that necessarily bad?
Yes, as I need the total time to calculate the speed
Have you noticed that the variable timevalue, which has the value zero, only ever appears on the right-hand side of assignments?
CtrlAltElite:
Have you noticed that the variable timevalue, which has the value zero, only ever appears on the right-hand side of assignments?
I'm sorry, but I don't understand. What do you mean?
If possible, can we private message each other to speed things up? I will later update my question to include a solution
Don’t PM at this stage, this an open, sharing forum to help the community experience and learn. The forum members are not here to do your homework for you - some members may choose to do so...
Add some print() calls to see what’s happening inside your code.
I can see several places your logic is falling off the tracks.
Don’t worry about timing accuracy yet, just get the sensors and interval detection sorted out.
Your effort is good for a beginner, but needs the persistence to push through to get results. If you started late, chances are you will finish late.
If possible, can we private message each other to speed things up?
No, certainly not - that would be rude for the other forum users.
If a variable is initialised with zero, and it never appears on the left hand side of an assignment, how does it ever get a value other than zero?
CtrlAltElite:
No, certainly not - that would be rude for the other forum users.
If a variable is initialised with zero, and it never appears on the left hand side of an assignment, how does it ever get a value other than zero?
I wrote 'counter=counter+1'. My problem as it seems now is that it is exiting my 'if' clause without waiting for 'photo2' to be 500 or above, thus stopping the counter at 1
I'm not talking about counter (which you should have written "counter++" without all the extraneous () parentheses), I'm talking about timevalue.
And please don't PM me again.
CtrlAltElite:
I'm not talking about counter (which you should have written "counter++" without all the extraneous () parentheses), I'm talking about timevalue.
And please don't PM me again.
Ok, I see. But I'm still at a loss at what to do. Like I said, I'm still a beginner. What should I do?
CtrlAltElite:
And please don't PM me again.
Sorry 'bout that
Try post #11 as a beginning.
What should I do?
Post your corrected code with some comments of what you expect to happen, and try running the code with the print statements suggested.
CtrlAltElite:
Post your corrected code with some comments of what you expect to happen, and try running the code with the print statements suggested.
int photoPin = A0;
int photoPin2 = A1;
int photo = HIGH;
int photo2 = HIGH;
int LED1 = 3;
int LED2 = 4;
int LED3 = 6;
int LED4 = 7;
int MS;
int KMH;
int distance = 1;
int counter;
int timeValue;
void setup() {
pinMode(photoPin, INPUT);
pinMode(photoPin2, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
Serial.begin(9600);
}
void loop() {
counter = 0;
photo = analogRead(photoPin);
photo2 = analogRead(photoPin2);
Serial.println(photo);
Serial.println(photo2);
delay(1000);
if (photo >= 500); { //when the sensor 'photo' becomes equal to or bigger than 500, start the counter, adding 1 every second
(counter = counter + 1);
delay(1000);
Serial.println(counter);
if (photo2 >= 500) { //when the sensor 'photo2' becomes equal to or bigger than 500, leave this clause
}
}
counter = timeValue; //whatever the counter value is, consider it as timeValue
Serial.println(timeValue); //print timeValue
}
MS = timeValue / distance; //find speed
KMH = MS * 3.6;
Serial.print(KMH);
if (KMH >= 50) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
}
if (photo2 >= 500) { //when the sensor 'photo2' becomes equal to or bigger than 500, leave this clause
}
This won’t do anything.
Try CTRL+T to auto format and indent your code properly.