Go Down

Topic: Help with basics (Read 440 times) previous topic - next topic

jgreene33

     Hello,
I have been working on this code. mainly trying to learn some things about loops. Im guessing someone will spot my mistake fairly easy.

Code: [Select]
int frank1 = 8;
int peter1 = 9;
int led3 = 10;
int led4 = 11;

void setup()

{Serial.begin(9600);
 pinMode(frank1, OUTPUT);
 pinMode(peter1, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);}

void loop()

{ void frank();
  void peter();
  void paul();
  void bobby();}
 
void frank()

{ if(Serial.available() >1)
 {  digitalWrite(frank1, HIGH);
    delay(300);
    digitalWrite(frank1, LOW);
    delay(300);}}

void peter()

{ if(Serial.available() >1)
 {  digitalWrite(peter1, HIGH);
    delay(300);
    digitalWrite(peter1, LOW);
    delay(300);}}

void paul()

{ if(Serial.available() >1)
 {  digitalWrite(led3, HIGH);
    delay(300);
    digitalWrite(led3, LOW);
    delay(300);}}

void bobby()

{ if(Serial.available() >1)
 {  digitalWrite(led4, HIGH);
    delay(300);
    digitalWrite(led4, LOW);
    delay(300);}}






   

groundFungus

What do you expect to happen? 

larryd

#2
Mar 20, 2017, 02:57 am Last Edit: Mar 20, 2017, 03:02 am by LarryD
Whenever more than one character is received, Serial.available() >1 will be true.
Since you never read those characters, Serial.available remains >1 hence the functions will execute forever.

What are you trying tho do.

Put { and } on lines by themselves.
Use CTRL T to format your code.

void frank(); this is not the way to call a function, it is a prototype.
.
No technical PMs.
The last thing you did is where you should start looking.

jgreene33

oh ok. I see why you dont get it. Im sorry thats my bad. I was only using serial to as a trigger. Is that ok to do?

larryd

#4
Mar 20, 2017, 03:08 am Last Edit: Mar 20, 2017, 03:08 am by LarryD
You can, but I don't see the reason to do this.

void frank(); this is a function prototype, the IDE does not need you to do it.
frank(); this calls the actual function.


.
No technical PMs.
The last thing you did is where you should start looking.

jgreene33

It's merely for learning. Understanding.

The way I set the {and} was just to make it easier for me to read. will this change the way my code works? I try to keep them in order.

control-T big thanks. point noted.

As for void frank(), I have been seeing things set this way here take a peek.

too long to copy:

http://forum.arduino.cc/index.php?topic=223286.0

thank you!
 

holmes4

Quote
As for void frank(), I have been seeing things set this way here take a peek.
As may be, but your still WRONG!

Just as you where in your other thread about the same problem http://forum.arduino.cc/index.php?topic=463986.0

Mark

larryd

#7
Mar 20, 2017, 03:29 am Last Edit: Mar 20, 2017, 03:31 am by LarryD
Since you are learning the basics, start to follow good programming techniques.
Place { and } on separate lines.

void frank();
is quite different from
frank();


Do you see the difference in the following three examples?

void frank(); <-----<<<< this is a function prototype
frank(); <-----<<<< this is how to call the function

void frank() <-----<<<< this is the function
{
. . .
}


.
No technical PMs.
The last thing you did is where you should start looking.

jimLee

Ran into this just last night.

called a function in my setup

Code: [Select]

bla;
bla;
void fileCleaner();
bla;
bla;


fileCleaner() got a real good going over, cleanup and rewrite before I realized my mistake. The compiler didn't care that it was in the middle of the code. But it never got called written that way.

-jim lee
PNW Ardiuno & Maker club
1012 9Th Street, Anacortes, WA 98221 (Around the back of building)
Contact jimLee or Naneen for more info

larryd

That will happen a few times until it won't anymore ;)


.
No technical PMs.
The last thing you did is where you should start looking.

Groove

Ran into this just last night.

called a function in my setup

Code: [Select]

bla;
bla;
void fileCleaner();
bla;
bla;


fileCleaner() got a real good going over, cleanup and rewrite before I realized my mistake. The compiler didn't care that it was in the middle of the code. But it never got called written that way.

-jim lee
It's also worth remembering that the function "bla" never got called either.
Per Arduino ad Astra

larryd

#define bla Serial.println("High Groove")

.
No technical PMs.
The last thing you did is where you should start looking.

Groove

#define bla Serial.println("High Groove")

.
Respect. 8)
Per Arduino ad Astra

jimLee

@jrgreen, if you are still here and are serious about learning the basics.

IF you are and IF you can attempt to solve a computer game WITHOUT looking up the solution on the net..

'Cause if you look it up its just wasted effort.

Grab this game : Human resource machine

And play it without googling anything.

It'll help. Just like "Wax on, Wax off"

-jim lee
PNW Ardiuno & Maker club
1012 9Th Street, Anacortes, WA 98221 (Around the back of building)
Contact jimLee or Naneen for more info

GoForSmoke

This works and leaves output in Serial Monitor (set it to 250000 baud).
You can enter text over and over and see how it acts, not just go by leds flashing.

I added a loop in setup() since you want to learn more about those.

Be sure to read the comments and ask about what you didn't 'get'.

Code: [Select]

int frank1 = 8;
int peter1 = 9;
int led3 = 10;
int led4 = 11;

void frank()
{
  if (Serial.available()) // not 0 is true
  { // this runs if there's data
    char ch = Serial.read(); // read one removes one
    // if you don't remove the char it stays available
    Serial.print( "Frank " );
    Serial.println( ch );

    digitalWrite(frank1, HIGH );
    delay(300);
    digitalWrite(frank1, LOW);
    delay(300);
  }
}

void peter()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    Serial.print( "Peter " );
    Serial.println( ch );

    digitalWrite(peter1, HIGH);
    delay(300);
    digitalWrite(peter1, LOW);
    delay(300);
  }
}

void paul()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    Serial.print( "Paul S " );
    Serial.println( ch );

    digitalWrite(led3, HIGH);
    delay(300);
    digitalWrite(led3, LOW);
    delay(300);
  }
}

void bobby()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    Serial.print( "Bobby " );
    Serial.println( ch );

    digitalWrite(led4, HIGH);
    delay(300);
    digitalWrite(led4, LOW);
    delay(300);
  }
}

byte countUp; // default is 0

void setup()
{
  Serial.begin( 250000); // set Serial Monitor to match
  Serial.println( "\nCounting with a LOOP" );

  while ( countUp < 10 )
  {
    Serial.println( countUp );
    countUp++; // adds 1 to countUp
  }

  Serial.println( "\n now enter text from Serial Monitor\n" );
}

void loop()
{ // this brace and the } below BELONG to void loop()
  // put right after, it's easier to see that.

  frank();
  peter();
  paul();
  bobby();
} // this } is on the same level as the matching {
// that way it is easier to match them up

// it is not "cool" to pack code in ways that make
// it harder to quickly see where one thing begins
// and ends. it's really kind of "fool yourself"
// SMART is making and keeping things clear

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up