I have created my own programme but I don't understand how I can use a push button to start and stop my robot.
I give you the code.
sorry for the english mistake, I'm French.
VOITURE_FINALE.ino (5.35 KB)
I have created my own programme but I don't understand how I can use a push button to start and stop my robot.
I give you the code.
sorry for the english mistake, I'm French.
VOITURE_FINALE.ino (5.35 KB)
MerlinLec:
I have created my own programme but I don't understand how I can use a push button to start and stop my robot.
I give you the code.
sorry for the english mistake, I'm French.
Have you used the "button" example in the IDE? Once you have tried it and get you button wired correctly, you will have a good idea how to add the button to your robot code.
Paul
There are no comments in your code and it looks like there are some pin definition conflicts
.
.
.
#define segC 4
#define segD 5
#define segE 6
#define segF 7
.
.
.
#define in1 6
#define in2 7
#define in3 4
#define in4 5
.
.
.
If you're trying to print the value of C to a seven-segment display either you've wired your 7-seg wrong or your digits aren't showing properly. There are much more elegant ways to display values on such a digit. For example, that massive "if/else" sequence could be replaced with:
const byte DigitPatterns[] =
{
//-gfedcba
0b00111111, //0
0b00000110, //1
0b01011011, //2
0b01001111, //3
0b01100110, //4
0b01101101, //5
0b01111100, //6
0b00000111, //7
0b01111111, //8
0b01100111 //9
0b01110111 //A
};
.
.
.
void Write7Seg( unsigned byte value )
{
if( value > 10 )
value = 10;
for( i=0; i<7; i++ )
digitalWrite( segA+i, (DigitPatterns[value] & (1<<i))?HIGH:LOW );
}//Write7Seg
so the first part of loop() becomes this:
if (distance >=11 )
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, 150);
analogWrite(enB, 150);
digitalWrite(LEDR, LOW);
digitalWrite(LEDL, LOW);
Serial.println("Trigger");
digitalWrite(LEDPin1, LOW);
digitalWrite(LED2, HIGH);
delay(200);
delay(50);
Write7Seg(C);
}//if
You've got tons of delays() in there too which make it difficult to establish a smooth loop() flow; it's hard to know where a button read and decision would even go.
I'd like to help but without understanding what you're doing with random numbers and whatnot, I'd just be guessing.
Blackfin:
There are no comments in your code
You're not big on comments either, and most of the code you throw out in response to beginners' questions contains pretty advanced concepts.
But then, by your own admission you're
Blackfin:
not a programmer by trade
[quote author=Willpatel_Kendmirez link=msg=4021525 date=1547698802]
You're not big on comments either, and most of the code you throw out in response to beginners' questions contains pretty advanced concepts.
I have found that many people here seem so jaded, fed-up and ill-tempered with newer users. They're crass, mean-spirited and rude.
I usually post decent code that, while not full-bore C++, suits the hobbyist and, yeah, maybe shows them a different way of doing common things. If I'm going to help someone asking how to make a light blink or do timing I'm not going to nausiate myself with showing on/delay/off crap that the person is just going to have to unlearn later.
Yes, I don't comment my code enough and I'll work on that as time permits. Mea culpa.
For whatever reason you chose to assail me for pointing out the lack of comments and generally hard-to-follow logic in the OP -- fair enough -- but do you take the time to go after those being rude, crass dicks to noObs too?
Blackfin:
but do you take the time to go after those being rude, crass dicks to noObs too?
I do actually: just 15 minutes ago, for example, I PM'd a 15k or so post member on exactly that.
Perhaps I shouldn't have PM'd though and have called him out in the forum.
Blackfin:
[quote author=Willpatel_Kendmirez link=msg=4021525 date=1547698802]
You're not big on comments either, and most of the code you throw out in response to beginners' questions contains pretty advanced concepts.
Yes, I don't comment my code enough and I'll work on that as time permits. Mea culpa.
The code should speak for itself. Instead of writing a comment "This next part does that" why not extraxt a function that is named after what is happening. Comments and code have the habit to become unrelated as time passes, as programmers tend to quickly change the code, but update the comments "later" (aka never).
LightuC:
The code should speak for itself. Instead of writing a comment "This next part does that" why not extraxt [sic] a function that is named after what is happening.
Agreed, but that assumes the reader knows what any individual line of code inside such a function does, and a beginner won't, necessarily
Blackfin is keen for example on the ternary operator, which is a simple enough operator to understand once you find its man page, but it's difficult to look up in a reference if you don't know what it's called. So a simple //that's called the ternary operator type comment would allow the neophyte to better understand Blackfin's showy code.
I have found a solution but thank for your help .