Show Posts
|
|
Pages: [1] 2 3 ... 10
|
|
1
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Defining a struct array
|
on: November 27, 2009, 12:00:56 pm
|
Hi, typedef struct { int one; int two; int three; } record_type;
record_type record[8];
void setup() { }
void loop() { record[0].one = 1; record[0].two = 2; record[0].three = 3; record[1].one = 4; record[1].two = 5; record[1].three = 6; record[2] = (record_type) {1,2,3}; }
Regars, Mike
|
|
|
|
|
2
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Hysteresis and How to Detect Peak Analog Value
|
on: January 12, 2010, 12:52:32 pm
|
Hi, you are right, sorry for the mistake: int findPeak() { int cycles = 0; int newVal1 = 0;
// variable to store whether the peak has been detected int peakNotFound = 1; // variable to store new values from the pot int newVal = 0; // variable to store current values from the pot int val = 0; // variable to store the peak value int peak = 0; // variable for loop counter int count = 0; // amount of hysteresis int hysteresis = 3;
// Clear the LCD and position cursor @ top-left // FYI: the LCD was initialized in the main program lcd.clear(); // Print a message to the LCD. // top row message lcd.print("Monitor Peak....");
while (peakNotFound) { // read pot 10 times and get the sum // "potPin" was defined in main program as shown below: // "const int potPin = 0;" for (count = 0; count < 10; count++) { newVal = newVal + analogRead(potPin); } // get average of readings and remap from 0-1023 to 0-255 newVal = (newVal / 10); newVal = map(newVal, 0, 1023, 0, 255); newVal1 += newVal; cycle++; // DEBUG code -------------- Serial.print("val = "); Serial.println(val, DEC); Serial.print("newVal = "); Serial.println(newVal, DEC); // -------------------------
// print out the value to LCD // LCD was cleared in vibrateMotor() // set the cursor to bottm row (column 0, line 1) lcd.setCursor(0, 1); // bottom row formatting: Cyl 1 Temp = xxx // bottom row message lcd.print("Cyl 1 Temp = "); lcd.print(newVal, DEC); // if temp transitions from 100 to 99, add a space at the end // of the LCD-displayed value to remove remnant trailing "0" if (newVal < 100) { lcd.print(" "); }
if (cycle == 10) // 10 cycles measured { newVal1 := newVal1/10; // !!!!! must be newVal1 in the followinf statement if ((val-newVal1) > Hysteresis) { peak = val;
// DEBUG Serial.print("peak = "); Serial.println(peak, DEC);
// peakNotFound is now false peakNotFound = 0; } // set old value to new value //!!! also in the next statement val = newVal1; cycle = 0; }
// forced to add a half-second delay or "val" and "newVal" will // almost always be the same (thus never exceeding the // hysteresis) delay(50); // 10 cycles og 50ms gives also 500ms } // return the value of peak to main program return peak; }
hope this helps Mike
|
|
|
|
|
3
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Hysteresis and How to Detect Peak Analog Value
|
on: January 11, 2010, 01:00:55 pm
|
Hi, i would try it this way: int findPeak() { int cycles = 0; int newVal1 = 0;
// variable to store whether the peak has been detected int peakNotFound = 1; // variable to store new values from the pot int newVal = 0; // variable to store current values from the pot int val = 0; // variable to store the peak value int peak = 0; // variable for loop counter int count = 0; // amount of hysteresis int hysteresis = 3; // Clear the LCD and position cursor @ top-left // FYI: the LCD was initialized in the main program lcd.clear(); // Print a message to the LCD. // top row message lcd.print("Monitor Peak...."); while (peakNotFound) { // read pot 10 times and get the sum // "potPin" was defined in main program as shown below: // "const int potPin = 0;" for (count = 0; count < 10; count++) { newVal = newVal + analogRead(potPin); } // get average of readings and remap from 0-1023 to 0-255 newVal = (newVal / 10); newVal = map(newVal, 0, 1023, 0, 255); newVal1 += newVal; cycle++; // DEBUG code -------------- Serial.print("val = "); Serial.println(val, DEC); Serial.print("newVal = "); Serial.println(newVal, DEC); // -------------------------
// print out the value to LCD // LCD was cleared in vibrateMotor() // set the cursor to bottm row (column 0, line 1) lcd.setCursor(0, 1); // bottom row formatting: Cyl 1 Temp = xxx // bottom row message lcd.print("Cyl 1 Temp = "); lcd.print(newVal, DEC); // if temp transitions from 100 to 99, add a space at the end // of the LCD-displayed value to remove remnant trailing "0" if (newVal < 100) { lcd.print(" "); } if (cycle == 10) // 10 cycles measured { newVal1 := newVal1/10; if ((val-newVal) > Hysteresis) { peak = val; // DEBUG Serial.print("peak = "); Serial.println(peak, DEC); // peakNotFound is now false peakNotFound = 0; } // set old value to new value val = newVal; cycle = 0; } // forced to add a half-second delay or "val" and "newVal" will // almost always be the same (thus never exceeding the // hysteresis) delay(50); // 10 cycles og 50ms gives also 500ms } // return the value of peak to main program return peak; }
Mike
|
|
|
|
|
4
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Count number of times pin goes high per minute
|
on: January 02, 2010, 04:38:24 pm
|
Hi, here's your code changed: int ledPin = 13; // LED connected to digital pin 13 int photoPin = 2; // int val = 0; int valold = 0; int start; unsigned int count = 0;
void setup() // run once, when the sketch starts { pinMode(ledPin, OUTPUT); // sets the digital pin as output pinMode(photoPin, INPUT); valold = digitalRead(photoPin); Serial.begin(9600); }
void loop() // run over and over again { start = millis(); // take the start time count = 0; // reset couter // do the following loop for 60000ms = 1min while (millis()-start < 60000) { // check for overflow of millis() if (start > millis() { start = millis(); count = 0; } val = digitalRead(photoPin); if (val==LOW) { digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, HIGH); if (val <> valold) { count ++; valold = val; } } } // 1 minute over. print conts Serial.println(count,DEC); }
Good luck Mike
|
|
|
|
|
7
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: List of previous values?
|
on: November 27, 2009, 12:30:17 pm
|
Hi, try this void setup(){ Serial.begin(9600); Serial.println("Type 'h' for a commands list and syntax"); }
void loop() { char c, t; int nnn; int lastval[26]; // array for last values of command a..z bool dolist = false;
if(Serial.available() > 0) // If there is data to read { c = Serial.read(); // Get a character
delay(50);
if(c >= 'a' && c <= 'z') // If it's a command { nnn = 0; while(Serial.available() > 0) // While there is still data { t = Serial.read(); // Read next character
if(t == ',' || t == ' ') continue; // Skip commas and spaces if(t >= '0' && t <= '9') // If it's a number { nnn *= 10; // Multiply previous value by 10 nnn += t - '0'; // Add the new digit } else break;
delay(50); } } }
// Now, do something with c and nnn do { switch(c) { // if list command, start list all commands from a..z if exist case 'l' : // list command starts if (!dolist) { // start with command a c = 'a'; // activate list command dolist = true; } break; case 's': if (!dolist) { lastval[c-'a'] = nnn; // save the last value analogWrite(6,nnn); } else { Serial.println("Rotation Fans @:"); Serial.println(lastval[c-'a']); } break; case 'f': if (!dolist) { lastval[c-'a'] = nnn; analogWrite(3,nnn); } else { Serial.println("Fog Volume Fan Speed @:"); Serial.println(lastval[c-'a']); } break; case 'u': if (!dolist) { lastval[c-'a'] = nnn; analogWrite(5,nnn); } else { Serial.println("Updraft Fan:"); Serial.println(lastval[c-'a']); } break; case 'r': if (!dolist) { lastval[c-'a'] = nnn; analogWrite(9,nnn); } else { Serial.println("Red Light @:"); Serial.println(lastval[c-'a']); } break; case 'g': if (!dolist) { lastval[c-'a'] = nnn; analogWrite(10,nnn); } else { Serial.println("Green Light @:"); Serial.println(lastval[c-'a']); } break; case 'b': if (!dolist) { lastval[c-'a'] = nnn; analogWrite(11,nnn); } else { Serial.println("Blue Light @:"); Serial.println(lastval[c-'a']); } break; case 'h': if (!dolist) { // list command sonly if list command not active Serial.println("Commands: s = Rotation Fans | u = Updraft Fan | f = Fog Volume Fan | r = Red Light | g = Green Light | b = Blue Light | = List Values"); Serial.println("Syntax: command, value"); Serial.println("Value = PWM Value between 0 and 255"); } break; // Add other letters here, with break after each one, a..z are allowed } // if list command active, next command character until z reached if (dolist && ++c >= 'z') dolist = false; } while (dolist); }
Mike
|
|
|
|
|
8
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Multiple iff statements....
|
on: November 25, 2009, 02:46:31 pm
|
Hi, here is the solution: void loop() { // first handle the led bar // read the volume: int sensorReading = analogRead(analogPinVol); // map the result to a range from 0 to the number of LEDs: int ledLevel = map(sensorReading, 120, 600, 0, ledCount); // loop over the LED array: for (int thisLed = 0; thisLed < ledCount; thisLed++) { // if the array element's index is less than ledLevel, // turn the pin for this element on: if (thisLed < ledLevel) { digitalWrite(ledPins[thisLed], HIGH); } // turn off all pins higher than the ledLevel: else { digitalWrite(ledPins[thisLed], LOW); } }
// then do the Play Led val = analogRead(analogPinPlay); if (val > 160) { digitalWrite(ledPinPlay, HIGH); } else { // digitalWrite(ledPinPlay, LOW); } }
Good luck Mike
|
|
|
|
|
9
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino + Servo
|
on: November 12, 2009, 03:16:45 pm
|
Hi, try this #include <Servo.h>
Servo myservo [4]; // create servo object to control four servos
int pos [4] = {0}; // variable to store the servo position int maxpos[4] = {180,120,220,180}; // max. position for each servo int dir[4] = {1}; // count direction for each servo
void setup() { myservo[0].attach(9); // attaches the servo on pin 9 to a servo object myservo[1].attach(8); // attaches the servo on pin 8 to a servo object myservo[2].attach(7); // attaches the servo on pin 7 to a servo object myservo[3].attach(5); // attaches the servo on pin 5 to a servo object }
void loop() { // process all 4 servos for (int i = 0; i < 4; i++) { // increment or decrement pos value pos[i] += dir[i]; // if the pos reaches the upper limit, change the direction to - if (pos[i] >= maxpos[i]) dir[i] = -1; // if pos reaches the lower end, change direction to + if (pos[i] <= 0) dir[i] = 1; // set the value for the servo myservo[i].write(pos[i]); } // now all servos have a new value, delay for 5ms delay(5); }
You can set a different max value for each of the servos. Mike
|
|
|
|
|
10
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: analog read sent as midi
|
on: November 01, 2009, 05:05:43 am
|
|
Hi,
i think you are wrong. At a baud rate of 31250 it takes about 300us to send a character. You send 3 characters for a MIDI command, this needs 1 ms. In your loop you have a delay of 10ms after each sending, so the sending is completely done before the next one starts. (this is true as long as you do not use flow control - MIDI does'nt use it).
Mike
|
|
|
|
|
12
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Am I asking too much?
|
on: October 31, 2009, 04:16:05 am
|
Hi, the problem might be the math operations you are doing in between. Your loop seems ok, but you can make it a little bit simpler like this: if (nextOut < millis()) { sendOut(); nextout = millis()+100; }
There is also a problem when the function millis() has an overrun. You can use a timer instead. Mike
|
|
|
|
|
13
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: smooth value increment
|
on: October 31, 2009, 04:10:15 am
|
Hi, where are these values comming from - analog input? If so, try to filter the input, for example: int tmpin, anain; tmpin = 0; for (i=0; i<32;i++) { tmpin += analogRead(pin); } anain = tmpin >> 5;
Use anain as the value used to fade your colors. Mike
|
|
|
|
|
15
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: How to calculate the angular velocity through MCU
|
on: October 27, 2009, 12:42:04 pm
|
Hi, i do not know what amplitude the sine wave of the sensor has, the arduino can only work with signals in the range of 0..5V. You can do one of the following: 1. build a converter which converts a frequency to a voltage and feed the output of this inverter to an analog input of the arduino (not so easy) 2. build a comparator (you can use an op-amp). The comparator compares the sensor signal to a fixed voltage. If for example the sine wave goes from -1V to +1V and the fixed voltage, the comparator compares it with is 0V, then the output of the comparator is +5V as long as the sine wave is above 0V and the output is 0V as long as the sine wave is below 0V. The output of the comparator can be feed directly to a digital input pin of the arduino. Here is a short code example: // the comparator output is feed to pin 3 #define frqPin 3 long timeStart, timeStop;
void setup() { Serial.begin(9600); Serial.println("Start measuring"); }
void loop() { // start with input low if (digitalRead(frqPin == 0) { // wait until pin goes high while (digitalRead(frqPin) == 0) {} // get the time when input goes high timeStart = micros(); // wait until input goes low again while (digitalRead(frqPin) == 1) {} // wait until input goes high again while (digitalRead(frqPin) == 0) {} // take the actual time in microseconds timeStop = micros(); // the timedifference is the the time between two rising // edges of the input pin (= 1 period) timeStart = timeStop-timeStart; // normally the result is > 0, otherwise the // function micros() had an overflow (every 70 min) // the discard this measuring (or correct the value) if (timeStart > 0) { // calculate frequency // 1000000 microseconds/second, f = 1/t double f = 1000000.0/timeStart; // report the measured frequency SerialPrintln(f); } } }
Hope this helps Mike
|
|
|
|
|