Show Posts
Pages: [1] 2 3 ... 18
1  Using Arduino / Programming Questions / Re: byte level boolean, calibrate crystal, eeprom as flash on: September 10, 2014, 05:26:21 am
thank you for the tips; It helps a lot. It really does. I think I can only understand half of what you are talking, but I will do my homework.  smiley-mr-green
2  Using Arduino / Programming Questions / Re: byte level boolean, calibrate crystal, eeprom as flash on: September 09, 2014, 12:50:02 pm
1 ) Which memory ? Is the SRAM (variables) or the code size (in flash, the sketch code) to the limit ?
You can use a 'struct' and define bits, and use every bit on its own. That will lower the SRAM usage, but will make the code size a lot bigger. Using 8-bit for boolean makes the smallest code size.
Why do you use 16-bit for boolean ? You can use 'bool' and 'boolean'.

2 ) Can you show us the sketch ? How do you use millis() for timing ?
There is a register in the ATmega8 chip to tune the crystal. As far as I remember it is the 'OSCCAL' register. I have used that in the past. It will work, as long as the crystal stays at room temperature. I must warn you: you can not change OSCCAL a lot, but only in small steps.
For accurate timing, use the DS3231. It is very accurate.

3 ) Yes, you can store data in EEPROM. However, the functions to use the EEPROM requires extra code. How many custom LCD characters do you use ?
I have stored a sequence of tasks in EEPROM, to be able to change them from the serial monitor on the computer. In the end the code size was about the same.
To shrink the code, you can omit a complete part of the sketch. For example, don't use the Serial library at all, or don't use floating point at all, and so on.

Use the ATmega328p microcontroller. It is kind of useless to spent many hours trying to get everything into an ATmega8. Use an ATmega8 for small tasks.

thank you a lot for your answer;

1, Memory as in flash memory. even with boolean, it costs me 1 byte, and I have few boolean, about 6 of them, so 6 bytbs, but if I am using 1 byte for these 6 boolean, then, I save 5 bytes;

2, DS3231 is out of the question; don't want to be too complicated. my millis code is standard.
Code:
  nowtimer=millis();
  if(nowtimer-pretimer>=1000) {
    clock[3]++;   
   
    if(timer==1){
    alarmt[3]--;
    }   

    pretimer=nowtimer;     
  } 

3, okay, I see
The custom character I want to save to eeprom is like this;
Code:
byte AL[8] = {
  0b00000,
  0b00100,
  0b01110,
  0b01110,
  0b01110,
  0b11111,
  0b00100,
  0b00000
}; 
3  Using Arduino / Programming Questions / Re: Need some help with a small school task on: September 09, 2014, 10:22:17 am
I think you need to remove the or || part of your code.
4  Using Arduino / Programming Questions / byte level boolean, calibrate crystal, eeprom as flash on: September 09, 2014, 10:17:21 am
I have few questions regarding my LCD clock project which has been done, but I am building a next generation.

1, memory is an issue for my atmega8. I believe there are 8 bits in one byte, so I want to know if its possible to use 1 byte for 8 Boolean value. As for now in my code, I am using int for boolean, and 8 of them costs me 16 bytes of memory.

2, having my clock running 5 days now, more than 1 minute inaccurate!!!  Since I use millis 1000 to count the time, will it work if I write a little timer and have it running 24 hr, and then compare the real-time with the arduino running time to get the difference and correct this problem. Is this possible??? And is my cellphones timer accurate ?? Planning to use the timer on my phone to do the comparison.

3, since there is less than 8kb of flash memory on the atmega chip that I am working on, will it help to store some data, namely, custom characters for my LCD to the eeprom and save 64bytes of memory??? 1 character is 8bytes
5  Using Arduino / Programming Questions / Re: Serial read problem on: September 07, 2014, 08:23:31 am
1, why x+1? array aren't supposed to start from 0? isn't     text[x++] = c;  the same thing as     text[1] = c; when it starts copying bytes from Serial.read()?

2,  text
  • = '\0';  // Add null terminator;  won't this overwrite the last character?

3,  why need to use another variable char c? since text array is already a char type;

1)  x++ means increment x AFTER the expression has been evaluated:
Code:
x = 0;
text[x++] = c;
// is equivalent to:
x = 0;
text[x] = c;
x += 1;

2) No, the value of x is incremented AFTER it is used.
Code:
x = 0;
text[x++] = c;
text[x] = '\0';
// is equivalent to:
x = 0;
text[x] = c;
x += 1;
text[x] = '\0';

3)  Using 'c' to store the received character is easier than storing it in the array and referring to it as element x-1:
Code:
x = 0;
text[x++] = Serial.read();
// Now that x has been incremented the character is in text[x-1];
if (text[x-1] == '\\')


Thanks, it cleared up everything now
6  Using Arduino / Programming Questions / Re: Serial read problem on: September 07, 2014, 06:23:06 am
Try this:
Code:
char text[70];
int x=0;

void setup(){
  Serial.begin(300);
}


void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    text[x++] = c;
    text[x] = '\0';  // Add null terminator
    if(c == '\\') {
      Serial.print ("received:");
      Serial.println (text); 
      x=0;
    }
    Serial.print("text: ");
    Serial.println(text);     

    Serial.print("text[x-1]: ");
    Serial.println(c);       

    Serial.print("x: ");
    Serial.println(x);     

    Serial.println("----------------------");
  }
}

This works perfectly, but I don't understand:
1, why x+1? array aren't supposed to start from 0? isn't     text[x++] = c;  the same thing as     text[1] = c; when it starts copying bytes from Serial.read()?

2,  text
  • = '\0';  // Add null terminator;  won't this overwrite the last character?

3,  why need to use another variable char c? since text array is already a char type;

thank you so much
7  Using Arduino / Programming Questions / Re: Serial read problem on: September 07, 2014, 05:55:31 am
Code:
      text[x]=Serial.read();
      x++;
      if(text[x]==char(92)){    //  \, dec: 92, hex: 5C, oct: 134, bin: 1011100 

You are filling in text
  • , changing x to x+1, and then comparing text[x+1] to '\'.

using '\' gives me errors when compiling:
Code:
Serial:14: error: missing terminating ' character
Serial.ino: In function 'void loop()':
Serial:15: error: expected `)' before ';' token
Serial.ino: At global scope:
Serial:21: error: expected constructor, destructor, or type conversion before '.' token
Serial:22: error: expected constructor, destructor, or type conversion before '.' token
Serial:24: error: expected constructor, destructor, or type conversion before '.' token
Serial:25: error: expected constructor, destructor, or type conversion before '.' token
Serial:27: error: expected constructor, destructor, or type conversion before '.' token
Serial:28: error: expected constructor, destructor, or type conversion before '.' token
Serial:30: error: expected constructor, destructor, or type conversion before '.' token
Serial:31: error: expected constructor, destructor, or type conversion before '.' token
Serial:40: error: expected declaration before '}' token

using x+1 does gives me text
  • equals to something, but text gives me nothing.
8  Using Arduino / Programming Questions / Serial read problem on: September 07, 2014, 05:17:54 am
trying to type something in serial monitor and have it prints out with ending of this char '\';  text
  • should be 1 when x is 1; 2 when x is 2; but it doesn't print anything out instead.


code
Code:
char text[70];
int x=0;
void setup(){
      Serial.begin(300);
     
}


void loop(){

      if(Serial.available()> 0){
      text[x]=Serial.read();
      x++;
      if(text[x]==char(92)){    //  \, dec: 92, hex: 5C, oct: 134, bin: 1011100 
      Serial.print ("received:");
      Serial.println (text); 
      x=0;
      }
      }
     
      Serial.print("text:");
      Serial.println(text);     
     
      Serial.print("text[x]");
      Serial.println(text[x]);       
     
      Serial.print("x:");
      Serial.println(x);     
     
      Serial.print("----------------------");
      Serial.println(); 

         
}


debug results
Code:
--------
text:1
text[x]
x:1
--------
text:12
text[x]
x:2
--------
text:123
text[x]
x:3
--------
9  Using Arduino / Programming Questions / Re: for loop increasing (Value) and decreasing (i) function on: September 04, 2014, 05:22:42 am
..put your number in a small array. Then use i, counting 0..3, as an index to the array

thanks. I see, but the main reason for me to use array here is to save memory; if another array is added, wouldn't it be counterproductive here? 
10  Using Arduino / Programming Questions / for loop increasing (Value) and decreasing (i) function on: September 04, 2014, 05:12:37 am
Just found out how great array with for loop is. it has saved many bytes of memory, and now I am rewriting everything with for loop code. Here I encounter a problem. I need to write a for loop, but I can't figure out a function to do so

for i= 5,4,3,2  // here is the for loop i: 5 to 2

x=(i-2);  // this is easy, 5-2=3;   4-2=2.... etc
3,2,1,0

y=??? //but this???
4,7,10,13

/// here are some of my thinking or attempts, but they don't work; so my question: is there any tips for doing thing like this? and can someone solve this problem for me?


///////
-5+9=4;
-4+9+2=7
-3+9+4=10
-2+9+5=13

-5+10(2x5)-1=4
-4+12(2X6)-1=7
-3+14(2x7)-1=10
-2+16(2X8)-1=13
11  Using Arduino / Programming Questions / Re: awesome EEPROM - Reset - overflow - Clock related question on: September 04, 2014, 01:57:38 am
Yes, your 49day problem is no problem.

You may however experience an issue with using the arduino as a clock, as you will find out that it won't be accurate and you will be resetting the time very frequently (which could be weekly or monthly depending on the degree of which you may suffer from obsessive compulsive disorder).

Adding a nice Real Time Clock (RTC) is a very inexpensive way to add more fun and learning to your project.  A decent one can be accurate to seconds in a year.  Particularly so if you are proud of your new LCD clock. smiley-red

this is my timing code; when millis() reset back to 0; 0 - (a large number) will never >= 1000; RTC is too expensive for me, considering my atmga8 is free and I build this clock from old components, but I will try that out later.

Code:
  nowtimer=millis();
  if(nowtimer-pretimer>=1000) {
    sec++;   
   
    if(timer==1){
    asec--;
    }   

    pretimer=nowtimer;     
  } 
12  Using Arduino / Programming Questions / Re: awesome EEPROM - Reset - overflow - Clock related question on: September 04, 2014, 01:52:44 am
Quote
is it correct or work?
You're the one with it sitting in front of you - you tell us!

but to test it, I have to wait 45 days sitting in front of it.
13  Using Arduino / Programming Questions / Re: awesome EEPROM - Reset - overflow - Clock related question on: September 04, 2014, 01:49:15 am
Quote
but I do think after about 50 days, my arduino atmga8 's millis() will go back to zero.
So what?
My wall-clock goes through zero (midnight) once a day, and it has yet to cause me to reset at 23:59 or whatever.

Please use code tags when posting code.

alright, you are right, but will this idea of resetting my arduino actually or by any chance works?
14  Using Arduino / Programming Questions / Re: awesome EEPROM - Reset - overflow - Clock related question on: September 04, 2014, 01:47:29 am
No need to reset your arduino. If you write the code correctly then the roll over will not be an issue.

Here is my clock code. is it correct or work??

Code:
byte CK[8] = {
  0b00000,
  0b01110,
  0b10101,
  0b10101,
  0b10111,
  0b10001,
  0b01110,
  0b00000
}; 

byte AU[8] = {
  0b00100,
  0b00100,
  0b01110,
  0b11111,
  0b00100,
  0b01110,
  0b01110,
  0b01010};

byte AR[8] = {
  0b01000,
  0b00100,
  0b00110,
  0b11111,
  0b11111,
  0b00110,
  0b00100,
  0b01000};
 
byte AL[8] = {
  0b00000,
  0b00100,
  0b01110,
  0b01110,
  0b01110,
  0b11111,
  0b00100,
  0b00000
}; 

byte TI[8] = {
  0b00000,
  0b01110,
  0b00100,
  0b11111,
  0b00100,
  0b01010,
  0b10001,
  0b00000
};

 
#define au 0x01
#define ar 0x02
#define ck 0x03
#define al 0x04
#define ti 0x05

#define beep A5
#define led 7
#include "ClickButton.h"
#include <LiquidCrystal.h>

LiquidCrystal lcd(13,12,11,10,9,8);

//------------------------------------------------------------------------------------------------------

ClickButton ba(2, LOW, CLICKBTN_PULLUP); // cursor change -
ClickButton bb(3, LOW, CLICKBTN_PULLUP); // cursor change +
ClickButton bc(4, LOW, CLICKBTN_PULLUP); // okay button

int sec=0;
int minn=0;
int hr=0;
int day=0;

int asec=0;
int aminn=0;
int ahr=0;
int aday=0;

int timer=0;
int alarm=0;
char* sp="Start";

int state=0;

int menu=1;
int psmenu=1;

int post=1;
int pspost=1;

int arpost=0;
int xint=1000;
unsigned long nowtimer=0;
unsigned long pretimer=0;

unsigned long xnowtimer=0;
unsigned long xpretimer=0;

//------------------------------------------------------------------------------------------------------
void setup()
{

  lcd.begin(16,2);
  lcd.createChar(1, AU);
  lcd.createChar(2, AR); 
  lcd.createChar(3, CK);
  lcd.createChar(4, AL); 
  lcd.createChar(5, TI); 
  pinMode(led, OUTPUT);
  pinMode(beep, OUTPUT);
 
  ba.multiclickTime = 50;
  bb.multiclickTime = 50;   
  bc.multiclickTime = 50;
}


//------------------------------------------------------------------------------------------------------
void loop()
{
  ba.Update();
  bb.Update(); 
  bc.Update();
 
 
  if(alarm==1){
  digitalWrite(beep, state);
  }
  else
  digitalWrite(beep, LOW);
 
 
  nowtimer=millis();
  if(nowtimer-pretimer>=1000) {
    sec++;   
   
    if(timer==1){
    asec--;
    }   

    pretimer=nowtimer;     
  } 
 
 
  xnowtimer=millis();
  if(xnowtimer-xpretimer>=xint) {
   state=!state;   
   arpost++;   
   
   lcd.clear();
   xpretimer=xnowtimer;   
  }
 
 

  if(sec==60){  minn++;  sec=0; }
  if(minn==60){ hr++;  minn=0; }
  if(hr==24){ day++;  hr=0; }

  if(asec==-1){  aminn--;  asec=59; }
  if(aminn==-1){ ahr--;  aminn=59; }
  if(ahr==-1){ aday--;  ahr=23; } 
  if(aday==0 && ahr==0 && aminn==0 && asec==0 && timer==1) {timer=0; alarm=1;}
 
  if(arpost==16)arpost=0;
 
 
  if(timer==0){
    sp="Start";
    digitalWrite(led, LOW);
  }
  else {
    sp="Pause";
    digitalWrite(led, state);
  }
 

    if(menu!= psmenu){   //whenever menu changes, clear the lcd, set position back to 1;
    lcd.clear(); 
    post=1;
    psmenu=menu;
    }
   
    if(post!=pspost){  //whenever position changes, clear the lcd;
    lcd.clear();
    pspost=post;
    }

  //========== menu 1 ====================== K 00  00:00:00 K
  if(menu==1){
   
    char buffer[17];
    sprintf (buffer, "%c %02d%c %02d:%02d:%02d %c", ck, day, ti, hr, minn, sec, ck);
    lcd.setCursor(0,0);
    lcd.print (buffer);
   
    switch( post ){
 
    case 1:
    lcd.setCursor (arpost,1); //right arrow location 0-15;
    lcd.write(ar);
    if(bb.clicks==1) xint-=200;
    if(xint==200) xint=1000;
    break;
   
    case 2:
    lcd.setCursor (3,1);
    lcd.write(au);
    if(bb.clicks==1) day++;     
    break;   
   
    case 3:
    lcd.setCursor (7,1);
    lcd.write(au);
    if(bb.clicks==1) hr++;   
    break;

    case 4:
    lcd.setCursor (10,1);
    lcd.write(au);
    if(bb.clicks==1) minn++; 
    break;

    case 5:
    lcd.setCursor (13,1);
    lcd.write(au);
    if(bb.clicks==1)sec=0; 
    break;

    }
   
    if(post==6)post=1;
   
    if(bc.clicks==1) post++;
    if(bb.clicks==-1){ day=0; hr=0; minn=0; sec=0; }//long press reset   
  }
 

  //========== menu 2 ======================
  if(menu==2){
   
    char buffer[17];
    sprintf (buffer, "%c %02d%c %02d:%02d:%02d %c", al, aday, ti, ahr, aminn, asec, al);
    lcd.setCursor(0,0);
    lcd.print (buffer);
   
    switch( post ){
 
    case 1:
    if(alarm==0){
    char x[17];
    sprintf (x, "         [%s]", sp);
    lcd.setCursor (0,1);
    lcd.print(x);
    if(bb.clicks==1) timer=!timer;
    }
   
    if(alarm==1){
    lcd.setCursor (0,1);
    lcd.print("(: Time is Up :)"); 
    if(bb.clicks==1) {timer=0; alarm=0;}
    }
   
    break;
   
    case 2:
    lcd.setCursor (3,1);
    lcd.write(au);
    if(bb.clicks==1) aday++;     
    break;   
   
    case 3:
    lcd.setCursor (7,1);
    lcd.write(au);
    if(bb.clicks==1) ahr++;   
    break;

    case 4:
    lcd.setCursor (10,1);
    lcd.write(au);
    if(bb.clicks==1) aminn++; 
    break;

    case 5:
    lcd.setCursor (13,1);
    lcd.write(au);
    if(bb.clicks==1)asec++; 
    break;

    }
   
    if(post==6)post=1;
   
    if(bc.clicks==1) post++;   
    if(bb.clicks==-1) {  aday=0; ahr=0; aminn=0; asec=0; alarm=0; timer=0; }//long press reset
   
  }
 
  //================================ 
  /*if(post>2) {if(ba.clicks==1) post=1;}
  if(post==1) {if(ba.clicks==1) menu++;}
  */
 
  if(ba.clicks==1) menu++;
  if(menu==3) menu=1;
 

  if(bb.clicks==-1);
  if(bc.clicks==-1);
}

//------------------------------------------------------------------------------------------------------

15  Using Arduino / Programming Questions / awesome EEPROM - Reset - overflow - Clock related question on: September 04, 2014, 01:40:54 am
Hello there. I have built a LCD clock using a free atmga8 chip from a nice seller; everything works well, but I do think after about 50 days, my arduino atmga8 's millis() will go back to zero. It is a problem for my clock, because I am planning to have my clock running all year long on an AC adapter;  so I saw EEPROM, and I got an idea about reseting my arduino before 50 days, and use the EEPROM to update my clock automatically.  Also, I have optiboot on my atmga8, so reset time is about 2 sec. Will this work?

int resetPin=10;   //pin 10 is connected to a 1K resistor, and then to the resetPin;  <pin 10----1k Ohm -----reset Pin>; already have a 10k pull up resistor connect to reset;

void setup (){
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);


  // startup, read the value from eeprom, and set my time
  day=EEPROM.read (1); //day
  hr=EEPROM.read (2); //hr
  min=EEPROM.read (3); //min
  halfday=EEPROM.read (4); //halfday

}

if(day==45){            // when it's 45 days, record the timer data, and then reset pin go low to reset my arduino;

    EEPROM.write(1, day);
    EEPROM.write(2, hr);
    EEPROM.write(3, minn);
    EEPROM.write(4, halfDay); 
   digitalWrite(resetPin, LOW);
}
Pages: [1] 2 3 ... 18