Show Posts
|
|
Pages: 1 2 [3] 4
|
|
33
|
Using Arduino / Programming Questions / Re: Timer problem?!
|
on: February 23, 2011, 05:21:52 pm
|
No problem  forum is picky about 9500 chars so I took out some irrelevant code (methods are there). #include <nokia_3310_lcd.h>
// Temp/humidity display using nokia 3310 LCD display shield from nuelectronics.com // LCD Code based on source from mr. Greenbeast.
int pin = 1; // analog pin volatile int tempref = 0; int tempf=0; // temperature variables int samples[8]; // variables to make a better precision int maxi = -100,mini = 100; // to start max/min temperature
long lastTLswitch = 0;
#define TL_MAXTMP 28.0 #define TL_MINTMP 25.0
//keypad debounce parameter #define DEBOUNCE_MAX 15 #define DEBOUNCE_ON 10 #define DEBOUNCE_OFF 3
#define NUM_KEYS 5
#define NUM_MENU_ITEM 5
// joystick number #define UP_KEY 3 #define LEFT_KEY 0 #define CENTER_KEY 1 #define DOWN_KEY 2 #define RIGHT_KEY 4
// Pin used by Backlight, so we can turn it on and off. Pin setup in LCD init function #define BL_PIN 7
// Pin used by Tubelight, so we can turn it on and off. Pin setup in LCD init function #define TL_PIN 2
// menu starting points #define MENU_X 10 // 0-83 #define MENU_Y 0 // 0-5
// adc preset value, represent top value,incl. noise & margin,that the adc reads, when a key is pressed // set noise & margin = 30 (0.15V@5V) int adc_key_val[5] ={ 30, 150, 360, 535, 760 };
// debounce counters byte button_count[NUM_KEYS]; // button status - pressed/released byte button_status[NUM_KEYS]; // button on flags for user program byte button_flag[NUM_KEYS];
// menu definition char menu_items[NUM_MENU_ITEM][12]={ "TEMPERATURE", "HUMIDITY", "BACKLIGHT", "TUBELIGHT", "ABOUT" };
void (*menu_funcs[NUM_MENU_ITEM])(void) = { temperature, humidity, backlight, tubelight, about };
char current_menu_item; int blflag = 1; // Backlight initially ON volatile int tlflag = 0; // Backlight initially OFF char degree = 0x7b; // Degree symbol
Nokia_3310_lcd lcd=Nokia_3310_lcd();
int getTemp0();
void setup() {
Serial.begin(9600); // start serial communication // setup interrupt-driven keypad arrays // reset button arrays for(byte i=0; i<NUM_KEYS; i++){ button_count[i]=0; button_status[i]=0; button_flag[i]=0; }
// Setup timer2 -- Prescaler/256 TCCR2A &= ~((1<<WGM21) | (1<<WGM20)); TCCR2B &= ~(1<<WGM22); TCCR2B = (1<<CS22)|(1<<CS21);
ASSR |=(0<<AS2);
// Use normal mode TCCR2A =0; //Timer2 Overflow Interrupt Enable TIMSK2 |= (0<<OCIE2A); TCNT2=0x6; // counting starts from 6; TIMSK2 = (1<<TOIE2);
SREG|=1<<SREG_I;
lcd.init(); lcd.clear();
//menu initialization init_MENU(); current_menu_item = 0; lastTLswitch = millis(); Serial.println(lastTLswitch); }
void loop() { byte i; for(i=0; i<NUM_KEYS; i++) { if(button_flag[i] !=0) {
button_flag[i]=0; // reset button flag switch(i){ case UP_KEY: // current item to normal display lcd.writeString(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_NORMAL ); current_menu_item -=1; if(current_menu_item <0) current_menu_item = NUM_MENU_ITEM -1; // next item to highlight display lcd.writeString(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_HIGHLIGHT ); break; case DOWN_KEY: // current item to normal display lcd.writeString(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_NORMAL ); current_menu_item +=1; if(current_menu_item >(NUM_MENU_ITEM-1)) current_menu_item = 0; // next item to highlight display lcd.writeString(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_HIGHLIGHT ); break; case LEFT_KEY: init_MENU(); current_menu_item = 0; break; case RIGHT_KEY: lcd.clear(); (*menu_funcs[current_menu_item])(); lcd.clear(); init_MENU(); current_menu_item = 0; break; } } } }
/* menu functions */ void init_MENU(void) {
byte i; lcd.clear(); lcd.writeString(MENU_X, MENU_Y, menu_items[0], MENU_HIGHLIGHT );
for (i=1; i<NUM_MENU_ITEM; i++) { lcd.writeString(MENU_X, MENU_Y+i, menu_items[i], MENU_NORMAL); } }
// waiting for center key press void waitfor_OKkey() { byte i; byte key = 0xFF; while (key!= CENTER_KEY){ for(i=0; i<NUM_KEYS; i++){ if(button_flag[i] !=0){ button_flag[i]=0; // reset button flag if(i== CENTER_KEY) key=CENTER_KEY; } } } }
// Check if joystick is moved or pressed byte checkKeypressed() { byte key = 0xFF;
for(int i=0; i<NUM_KEYS; i++){ if(button_flag[i] !=0){ button_flag[i]=0; // reset button flag if(i== CENTER_KEY) key=CENTER_KEY; } } return key; }
char numStr[8];
void formatNum( int num ) { // Format a number to 2 decimal places }
// Display temperature in big digits, humidity in small digits underneath void temperature() { int temp, humid; int tempc2; tempc2 = getTemp0(); // Display non changing text, there is a slight delay while first reading is taken lcd.gotoXY( 62,1 ); lcd.print( tempc2 ); lcd.print( "C" ); // lcd.writeString(67, 1, "C", MENU_NORMAL); lcd.writeString(38, 5, "OK", MENU_HIGHLIGHT ); lcd.writeString(40, 3, "%RH", MENU_NORMAL);
long lastUpdate = 0; // Force update byte i; byte key = 0xFF;
// Loop to display temperaure/humidity with check for key press to exit while (key!= CENTER_KEY) { // Update temp if( millis() > lastUpdate + 1000) { // Read temperature and humidity
temp = (int) tempc2 * 100; humid = 10;
// Display temp formatNum( temp ); lcd.writeStringBig(8, 0, numStr, MENU_NORMAL);
// Display Humidity formatNum( humid ); lcd.writeString(10, 3, numStr, MENU_NORMAL);
lastUpdate = millis(); tempc2 = getTemp0();
// Serial.print("de temp is hier: "); // Serial.println(tempc2); } key = checkKeypressed();
} }
// Display humidity in big digits, temperature in small digits underneath void humidity() { int temp, humid; // show humidity, don't have that sensor in yet. }
// Allow backlight to be turned on and off void backlight() {
lcd.writeString( 0, 1, "Toggle", MENU_NORMAL); lcd.writeString( 0, 2, "Backlight", MENU_NORMAL); if( blflag != 0 ) { lcd.writeString( 60, 2, "Off", MENU_HIGHLIGHT); } else { lcd.writeString( 60, 2, "On", MENU_HIGHLIGHT); }
waitfor_OKkey();
if( blflag != 0 ) { blflag=0; digitalWrite( BL_PIN, LOW ); } else { blflag = 1; digitalWrite( BL_PIN, HIGH ); } }
// Allow tubelight to be turned on and off void tubelight() {
lcd.writeString( 0, 1, "Toggle", MENU_NORMAL); lcd.writeString( 0, 2, "TL Tube", MENU_NORMAL); if( tlflag != 0 ) { lcd.writeString( 60, 2, "Off", MENU_HIGHLIGHT); } else { lcd.writeString( 60, 2, "On", MENU_HIGHLIGHT); }
waitfor_OKkey();
if( tlflag != 0 ) { tlflag=0; digitalWrite( TL_PIN, LOW ); } else { tlflag = 1; digitalWrite( TL_PIN, HIGH ); } }
// The followinging are interrupt-driven keypad reading functions // which includes DEBOUNCE ON/OFF mechanism, and continuous pressing detection // Convert ADC value to key number
char get_key(unsigned int input) { char k;
for (k = 0; k < NUM_KEYS; k++) { if (input < adc_key_val[k]) { return k; } }
if (k >= NUM_KEYS) k = -1; // No valid key pressed return k; }
void update_adc_key() { // ... some code }
int getTemp0() { int tempc = 0; cli(); for(int i = 0;i<=15;i++){ // gets 8 samples of temperature samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0; tempc = tempc + samples[i]; delay(50); }
tempc = tempc/16.0; // better precision tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit if (tempc < (TL_MAXTMP)) { if (tempc < TL_MINTMP) { Serial.println(tlflag); Serial.print("Vorige keer: "); Serial.println(lastTLswitch); Serial.println(millis()); // this makes the code even freeze! if ((tlflag == 0) && ( millis() > (lastTLswitch + 2000))) { Serial.println("Heater on..."); tlflag = 1; digitalWrite( TL_PIN, HIGH ); lastTLswitch = millis(); } } } else { // if heater is off, leave off.... if ((tlflag == 1) && ( millis() > (lastTLswitch + 2000))) { tlflag = 0; digitalWrite( TL_PIN, LOW ); lastTLswitch = millis(); } }
if(tempc > maxi) { maxi = tempc; } // set max temperature if(tempc < mini) { mini = tempc; } // set min temperature
return tempc; sei();
}
// Timer2 interrupt routine - // 1/(160000000/256/(256-6)) = 4ms interval
ISR(TIMER2_OVF_vect) { TCNT2 = 6; tempref = ( 5.0 * analogRead(pin) * 100.0) / 1024.0; update_adc_key(); }
|
|
|
|
|
34
|
Using Arduino / Programming Questions / Timer problem?!
|
on: February 23, 2011, 04:54:43 pm
|
I'm having a problem with my application. The application is as follows: a thermostat device that switches on a heater at a certain minimum value and switches it off at an other value. The application is quite straight forward: - a Nokia LCD 3310- a LM35 temperatureSo far so good. I'm a programmer, so combining some examples with my own code was no problem. What is a problem is that I don't want the heater to switch on and off too fast. long lastTLswitch = 0;
// in the main loop:
if ((tlflag == 0) && ( millis() > (lastTLswitch + 2000))) { // switch on the heater } else if (millis() > (lastTLswitch + 2000)) { // switch off the heater }
The problem: 1376277 is the value in lastTLswitch even when I initialize it so it functions but with a timeout that is too long. If I print millis() it even freezes. I suspect that interrupt ISR(TIMER2_OVF_vect) causes this problem. I have looked around, but I didn't find the answer yet.
|
|
|
|
|
35
|
Forum 2005-2010 (read only) / Troubleshooting / usbdev_open(): did not find any USB dev on Mac
|
on: August 27, 2010, 01:01:36 pm
|
|
I'm trying to burn optiboot using an JTAGICE mkII programmer. On Windows I don't have arduino software installed, on Mac I get this error:
usbdev_open(): did not find any USB dev
Where can I modify the avrdude settings for Arduino so I can point it to tty.usbserial-A800ep7B instead of usb (as stated by Arduino [0018]) usbdev_open(): did not find any USB device
|
|
|
|
|
39
|
Forum 2005-2010 (read only) / Troubleshooting / Re: A strange problem with inputs
|
on: March 09, 2010, 06:23:36 am
|
I have tested some more. Connecting the pins to btnred = 19; btnyel = 18; btnblu = 17; btngrn = 16;
gives the same problem. If I turn the cable around it turns out to be that the blue button is seen by the arduino and becomes low (as is supposed to happen). Very strange isn't it, I can't make any sense out of it. [update] A duemilanove gives the same outcome.
|
|
|
|
|
41
|
Forum 2005-2010 (read only) / Troubleshooting / A strange problem with inputs
|
on: March 09, 2010, 04:45:45 am
|
I am connecting 4 buttons to an arduino pro At this moment I use the analog inputs, but no matter what the same problem keeps appearing: one of the buttons will not be detected. I have used my multimeter to ensure that it functions well (it becomes low when I press it): Below the code that I use: int btnred, btnyel, btngrn, btnblu; // buttons int pressed = LOW;
void setup() { // setting the pins btnred = 17; btnyel = 16; btnblu = 15; btngrn = 14;
pinMode(btnred, INPUT); digitalWrite(btnred, HIGH); pinMode(btnyel, INPUT); digitalWrite(btnyel, HIGH); pinMode(btngrn, INPUT); digitalWrite(btngrn, HIGH); pinMode(btnblu, INPUT); digitalWrite(btnblu, HIGH); //Serial.begin(9600);
}
void loop() { int redval, yelval, bluval, grnval; redval = digitalRead(btnred); yelval = digitalRead(btnyel); bluval = digitalRead(bluval); grnval = digitalRead(btngrn); if (redval == pressed) { lc.setDigit(0,3,1,false); //Serial.println("Yellow"); } else { lc.setDigit(0,3,0,false); } if (bluval == pressed) { lc.setDigit(0,1,1,false); //Serial.println("Yellow"); } else { lc.setDigit(0,1,0,false); } if (grnval == pressed) { lc.setDigit(0,0,1,false); //Serial.println("Yellow"); } else { lc.setDigit(0,0,0,false); } if (yelval == pressed) { lc.setDigit(0,2,1,false); //Serial.println("Yellow"); } else { lc.setDigit(0,2,0,false); } delay(200); }
B.t.w. I also seem to get crap when I use the terminal. I have tred using other pins, etc etc, then the blue button might function but an other one will fail to report when it is pressed.
|
|
|
|
|
42
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: controlling multiple output pins
|
on: March 09, 2010, 02:04:29 pm
|
|
I think you don't use millis() correctly. it will soon become larger than 7000 and then the code doesn't react anymore. I'm also not sure what < m < does, at least it is very unusual for programmers to use it like this (even if it would be correct but I'm not sure about that).
if (interval2 < m < interval5) should be written:
if ((interval2 < m) && (m < interval5)) {
}
|
|
|
|
|
44
|
Forum 2005-2010 (read only) / Syntax & Programs / Strange PWM behaviour.
|
on: October 01, 2010, 03:22:02 pm
|
Hi fellows, I have a strange 'problem'. I created a work around, but am still curious what could be the cause! I'm connecting an H-bridge (L298N) unit ( http://www.dfrobot.com/index.php?route=product/product&product_id=66) to my Arduino and use (amongst others) this code: uint8_t motor1_en = 9; uint8_t motor1_dir = 8; uint8_t motor2_en = 11; uint8_t motor2_dir = 10;
...
digitalWrite(motor2_dir, HIGH); analogWrite(motor2_en, motorspeed);
I was expecting (ergo: the information from that site has a sketch with that code), that the motors could be set using a value from 0..255 and setting the direction to low and/or high. But... when I test it using my motor on output 2: I need to set direction to HIGH and 0 is maximum speed forward, around 100 is minimum speed forward, 125 is about minimum speed backward and 255 is the maximum speed backward. That seems to be quite strange. I don't see any mistakes, I think that I have connected everything well. I know the atmega328 datasheet almost by heart (only 500+ pages  ).
|
|
|
|
|
45
|
Forum 2005-2010 (read only) / Syntax & Programs / Max7221 on analog pins with Duemilanove
|
on: February 27, 2010, 07:16:20 am
|
I would like to connect the Max7221 to some analog pins due to lack of other pins. Reading the datasheet and following articles, this should be possible, but it doesn't work for some reason: /playground/Main/MAX72XXHardware /en/Tutorial/AnalogInputPins I have connected: Data IN to pin 16 Load to pin 15 Clock to pin 14 Trying different things like making pins high and low, changing pins to in- and output didn't make it work. The print with LEDs does work when I connect it to normal pins (like 8, 9 and 10). Thanks a lot for any hints. Johan -- source -- #include "LedControl.h"
int pin1 = 14, pin2 = 15, pin3 = 16;
LedControl lc = LedControl(pin3, pin2, pin1, 1);
void setup() { lc.shutdown(0, false); lc.setIntensity(0,15);
Serial.begin(9600); }
void loop() { int i = 99; printNumber(i); // some method that lights up the display delay(100); }
|
|
|
|
|