Hallo allemaal,
Onlangs hebben we een projectje opgestart en hierbij een Arduino geprogrammeerd. In hardware zijn we sterk genoeg om ons mannetje te staan maar bij software short het nog wel eens..
In mijn beleving zijn variabelen die voor void setup en void loop worden gedeclareerd, voor iedere functie toegankelijk.
Echter, in ons programma moeten we in de functie switchoff alsnog de potvalue laten berekenen.
Het gaat dan met name om regel 81, als ik deze bovenin zet, wordt de variabele niet bewaard.
Als ik de variabele helemaal bovenin zet, dus buiten de context van de functie, geeft de functie de waarde 0 terug.
Als ik de variabele in de methode zet, wordt deze wel correct weergegeven.
Dit is in weze niet zo erg, het werkt zo, alleen vind ik het raar dat deze variabele niet globaal gezien wordt.
Mis ik soms iets?
Onderstaand de code, e.e.a. heb ik gekuisd
Onlangs hebben we een projectje opgestart en hierbij een Arduino geprogrammeerd. In hardware zijn we sterk genoeg om ons mannetje te staan maar bij software short het nog wel eens..
In mijn beleving zijn variabelen die voor void setup en void loop worden gedeclareerd, voor iedere functie toegankelijk.
Echter, in ons programma moeten we in de functie switchoff alsnog de potvalue laten berekenen.
Het gaat dan met name om regel 81, als ik deze bovenin zet, wordt de variabele niet bewaard.
Als ik de variabele helemaal bovenin zet, dus buiten de context van de functie, geeft de functie de waarde 0 terug.
Als ik de variabele in de methode zet, wordt deze wel correct weergegeven.
Dit is in weze niet zo erg, het werkt zo, alleen vind ik het raar dat deze variabele niet globaal gezien wordt.
Mis ik soms iets?
Onderstaand de code, e.e.a. heb ik gekuisd
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
| #include <LiquidCrystal.h> //make use of the HD44780 compatible LCD controller LiquidCrystal lcd(12, 11, 10, 9, 5, 4); //initialize the used pins by the LCD int optoH = 7; //Optocoupler High signal int optoL = 6; //Optocoupler Low signal int startbutton = 2; //Start of switchoff sequence int resetbutton = 3; //Reset after start sequence is completed int mosfetOut = 8; //Mosfet switch output from arduino, low = mosfet closed, high = mosfet open int sbuttonState = 0; //State of startbutton int rbuttonState = 0; //State of resetbutton float duration, pulseHigh, pulseLow, fre; //variabeles declared to be floating byte logo[8] = { //Logo of company B10100, B10000, B11010, B11010, B10011, B11111, B11111, }; void setup() { //Serial.begin(9600); pinMode(optoH, INPUT); pinMode(optoL, INPUT); pinMode(mosfetOut, OUTPUT); pinMode(startbutton, INPUT); pinMode(resetbutton, INPUT); // set up the LCD's number of columns and rows: lcd.createChar(0,logo); //initializes logo on LCD lcd.begin(16, 2); // Print a message to the LCD. lcd.print("XXXXXXXXXX"); lcd.setCursor(14,0); lcd.write(byte(0)); //writes logo to LCD screen lcd.setCursor(0, 1); lcd.print("AC peak detector"); delay(2000); lcd.clear(); } void loop() { //Serial.println('duration'); pulseHigh = pulseIn(optoH, HIGH); pulseLow = pulseIn(optoL,LOW); duration= pulseHigh+pulseLow; duration = duration / 1E6; fre=(1/duration); int potValue = analogRead(A0); lcd.setCursor(0,0); lcd.print(fre,1); lcd.print("Hz"); lcd.setCursor(0,1); lcd.print(potValue * 5); lcd.print(" usec delay"); //Serial.println(fre); //geeft de frequentie weer //Serial.println(potValue); //geeft de potmeter waarde weer // read the state of the pushbutton value: sbuttonState = digitalRead(startbutton); rbuttonState = digitalRead(resetbutton); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (sbuttonState == HIGH) { // check state of start button, if high, go to switchoff switchoff();// run switch off routine } else { if (rbuttonState == HIGH) { //check state of reset button, if high, go to reset reset(); //run reset routine } } } void switchoff() { int potValue = analogRead(A0); int timeout = (potValue * 5); //int optoH = 7; if (pulseIn(optoH,HIGH)) { delayMicroseconds(timeout); digitalWrite(mosfetOut,LOW); } } void reset() { digitalWrite(mosfetOut,HIGH); } |
[ Voor 3% gewijzigd door Looney11 op 02-05-2013 17:17 . Reden: verwarrende zin aangepast. ]