Log ind for at få flere funktioner

  • 11-06-2011 · 14:39 376 visninger 3 svar
  • Denne tråd er over 6 måneder gammel

    Er du sikker på, at du har noget relevant at tilføje?

  • Arduino question

    Af Girl in distress gæst
Hi

Can anybody help me figuring out which three different code blocks an Arduino sketch usually consist of? I'm kind of new to this...
HOL Moderator
11-06-2011 15:35
Please elaborate.
Is this a question from an assignment you've been given by a teacher, based on something you were supposed to read?
If so, I'm guessing that you were supposed to read this:
http://www.arduino.cc/en/Tutorial/S...

In which case the answer is Comments, Variables and Functions.

Depending on the context of the question, it could also be Variables, Setup() and Loop().
Girl in distress

#2

Gæst
11-06-2011 16:04
Okay.. it is an assigment, and the last thing you wrote (variables, setup, and loop) was also what my friends and I concluded. But because we did not fully understand what a code block was, we were not entirely sure of the answer. But thanks for the answer! :)
Semi Supporter
11-06-2011 16:52
You can also create a custom function you can "launch" from your main loop. You declare these functions with the "void" command, just like the main loop and setup functions

Ex. if you want to set the states for an LED using only one command instead of writing the same piece of code everytime, you can create a seperate function to handle the LED state:

void rgbLED(String color) // set color of RGB LED, r=red g=green b=blue
{
if (color == "r"){
digitalWrite(RGB_Rled, HIGH);
digitalWrite(RGB_Gled, LOW);
digitalWrite(RGB_Bled, LOW);
}
else if (color == "g"){
digitalWrite(RGB_Rled, LOW);
digitalWrite(RGB_Gled, HIGH);
digitalWrite(RGB_Bled, LOW);
}
else if (color == "b"){
digitalWrite(RGB_Rled, LOW);
digitalWrite(RGB_Gled, LOW);
digitalWrite(RGB_Bled, HIGH);
}
}

if you then call the rgbLED("X"); from ex. your loop(), the program would go to the void rgbLED, set the LED according to what you wanted, and return to the main loop again.

It is also possible to create functions, that returns an int when you call the function (ex if you only need to scan an analog input at certain points in your code)

void loop()
{
digitalWrite(Ri2c_led, LOW);
rgbLED("g");
cupLED("g");
lcd.init(); // initialize the lcd lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("DrinkMaster9000");
lcd.setCursor(0, 1);
lcd.print(" = Insert Cup = ");
delay(1000);
int irtr = chkIR(); // check if cup is inserted, return to loop (with LOW) if IR is blocked
}

int chkIR() {
int val = readMux(5); //IR transistor is connected to MUX channel 5
if(val>IRLOW && val
Bruger påkrævet
En bruger er påkrævet for at oprette svar på Hardwareonline.dk
Du kan oprette en bruger her eller logge ind her

Log ind for at få flere funktioner