Show Posts
Pages: [1] 2 3 ... 10
1  Using Arduino / Microcontrollers / Re: questions about tx and rx, usb-serial on: July 27, 2014, 05:32:21 pm
Why it's not Tx pin connect to Tx pin? it doesn't work with Tx connected to Tx, Rx to Rx.
Because when you connect two devices together which are "peers", what one transmits is received by the other - in each direction, so the connections must cross over.

also, I got one of those RF, radio frequency mini board. I use it on my uno to talk to my computer using this usb-ttl, but a lot of random code shows up. I think that is noise, right? how to avoid that?

Radio - especially AM radio - is noisy.  While your ears can find meaning amongst a modest degree of noise, digital communications systems need to be designed to manage it, and particularly to determine when something is being sent and when it is not.  This is achieved by "packet" protocols where packets contain synchronising patterns and checksums as well as the data, and mechanisms acknowledge and/ or repeat packets that fail to be received correctly.


I see.

it's one of these,  very cheap and useful.


Receiver module parameters

Product Model MX-05V
Operating voltage: DC5V
Quiescent current: 4mA
Receiving frequency: 433MHZ
Receiver sensitivity:-105DB
Size: 30 * 14 * 7mm
An extra antenna is needed, please use spiral 50Ω a quarter wavelength antenna
Technical parameters of the transmitter head

Product Model: MX-FS-03V
Launch distance :20-200 meters (different voltage, different results)
Operating voltage :3.5-12V
Dimensions: 19 * 19mm
Operating mode: AM
Transfer rate: 4KB/s
Transmitting power: 10mW
Transmitting frequency: 433M
Pinout from left to right: (DATA; VCC; GND)
2  Using Arduino / Programming Questions / Re: Need help/advice in 2x16 LCD menu on: July 27, 2014, 04:23:37 am
the rest of the code

Code:
void setup() {
  
  //===5 RED LEDs ===
for(int x=1; x<7; x++){
  pinMode(ledPin[x],OUTPUT);
}  
  //===4 Black buttons ===  
for(int x=1; x<5; x++){
  pinMode(buttonPin[x], INPUT_PULLUP);
}  
  //===Sensors ===
  pinMode(shuntPin, INPUT);// shunt voltage  
  pinMode(tempPin, INPUT);// Temp. Sensor  
  pinMode(lightPin, INPUT);// Light resistor  
  pinMode(beeperPin, OUTPUT); // Beeper  
  
  //===LCD SETUP ===  
  lcd.begin(16,2);
  lcd.createChar(7, timeIcon);
  lcd.createChar(1, tempIcon);
  lcd.createChar(2, batteryIcon);  
  lcd.createChar(3, lightIcon);  
  lcd.createChar(4, arrowupIcon);  
  lcd.createChar(5, buttonIcon);  
  lcd.createChar(6, alarmIcon);  
  
}

//====================================================================================================================================
//====================================================================================================================================
void loop() {

  
  
//------------------------------------------------------------------>>LCD
if(mode!=lastMode){
  lastMode=mode;  
  if(subMode>=1){
  subMode=0;
  mode--;  
  }
}

if(mode==1){

   if(subMode==0){
  char x [16];
  sprintf(x, "%c%02d:%02d:%02d:%02d %02d%c", timeI,DAY,HR,MIN,SEC,tempC,tempI);
  lcdPrintText(0,0,x);
    
  char y [16];
  sprintf(y, "%cm %c- %c+ %cx     ", buttonI,  buttonI, buttonI, buttonI );
  lcdPrintText(0,1,y);
   }
    
  if(subMode==1){
    
  char x [16];    
  sprintf(x, "%c%02d:%02d:%02d:%02d %02d%c", timeI,DAY,HR,MIN,SEC,tempC,tempI);
  lcdPrintText(0,0,x);  
  
  if(subsubMode==1){
    char y [16];
    sprintf(y, "%c Set Time      ", arrowupI );
    lcdPrintText(0,1,y);    
  }
  
  if(subsubMode==2){
    char y [16];
    sprintf(y, "  %c          Day", arrowupI );
    lcdPrintText(0,1,y);    
  }  
  
  if(subsubMode==3){
    char y [16];
    sprintf(y, "     %c       Hour", arrowupI );
    lcdPrintText(0,1,y);    
  }

  if(subsubMode==4){
    char y [16];
    sprintf(y, "        %c   Minute", arrowupI );
    lcdPrintText(0,1,y);    
  }    
  
  if(subsubMode==5){
    char y [16];
    sprintf(y, "     (C/F)        %c", arrowupI );
    lcdPrintText(0,1,y);    
  }    
  
  if(subsubMode==5){
    char y [16];
    sprintf(y, "     (C/F)        %c", arrowupI );
    lcdPrintText(0,1,y);    
  }      

  if(subsubMode==5) subsubMode==0;
  }
  
  
  if(subMode==2){
  char x [16];    
  sprintf(x, "%c%02d:%02d:%02d:%02d %02d%c", timeI,DAY,HR,MIN,SEC,tempC,tempI);
  lcdPrintText(0,0,x);  
  
  if(arrowLoc[0]==1){
  char y [16];
  sprintf(y, "%c Set Alarm 1   ", arrowupI );
  lcdPrintText(0,1,y);  
  }
  }  
  
  if(subMode==3){
  char x [16];    
  sprintf(x, "%c%02d:%02d:%02d:%02d %02d%c", timeI,DAY,HR,MIN,SEC,tempC,tempI);
  lcdPrintText(0,0,x);  
  
  if(arrowLoc[0]==1){
  char y [16];
  sprintf(y, "%c Set Alarm 2   ", arrowupI );
  lcdPrintText(0,1,y);  
  }
  }    
  
  if(subMode==4)  subMode=0;
  if(subMode==-1)  subMode=4;  
  
}

if(mode==2){
  
  char x [16];
  sprintf(x, "%c%02d:%02d:%02d%c% 02d %c%c", batteryI,bdHR,bdMIN,bdSEC,batteryI,tempC,alarmI,lightI);
  lcdPrintText(0,0,x);
    
  char y [16];
  sprintf(y, "%04d:mAh %04d:mA", shuntMah,shuntMa );
  lcdPrintText(0,1,y);
}

if(mode==3){
  
  char x [16];
  sprintf(x, "%c%02d:%02d:%02d:%02d%c %c%c% ", timeI, cdDAY,cdHR,cdMIN,cdSEC,timeI, alarmI,lightI);
  lcdPrintText(0,0,x);
    
  char y [16];
  lcdPrintText(0,1,"Day Hr Min TIMER");  

}

if(mode==4){
  
  char x [16];;
  sprintf(x,"%c Pigs Arduino %c",arrowupI,arrowupI);
  lcdPrintText(0,0,x);
    
  char y [16];
  lcdPrintText(0,1," Mini Os V.1.00 ");  

}

if(mode==5)mode=1;

/*


  char tempRead[5];
  sprintf(tempRead, "%02d %02d", lightLevel,tempC);  
  lcd.setCursor(10, 0);    
  lcd.print(tempRead);    
  
  char shuntRead[14];
  sprintf(shuntRead,"%04dmA %04dMah",shuntMa, shuntMah);
   lcd.setCursor(1, 1);
  lcd.print(shuntRead);  
  
  */
    
//------------------------------------------------------------------<<LCD


//------------------------------------------------------------------>>Time
unsigned long currentSec=millis();
if(currentSec-previousSec>1000) {
SEC++;
previousSec=currentSec;
if(shuntMa>1){shuntMatotal=shuntMatotal+shuntMa;}
}

if(SEC==60){SEC=0; MIN++;}
if(MIN==60){MIN=0; HR++;}
if(HR==24) {HR=0;DAY++;}
//------------------------------------------------------------------<<Time




//------------------------------------------------------------------>> Sensors
// 8% difference from meter to arduino reading
int shuntVal= analogRead(shuntPin);
shuntMa=shuntVal*(5.0/1023.0*1000.0*0.92);              //shuntMa
shuntMah=shuntMatotal/3600;                            //shuntMah

//(21%) tempVal*0.21=F degree/ (F-32)X5/9=C degree
int tempVal= analogRead(tempPin);
tempF=tempVal*0.21;
tempF= constrain (tempF, 0,99);                      // tempF
tempC=((tempVal*0.21)-32)*5/9;                       // tempC

//darkest 50-1010 lightest
int lightVal= analogRead(lightPin);
lightVal=map(lightVal, 50,1010,0,99);
lightLevel=constrain (lightVal, 0, 99);             // lightLevel

//------------------------------------------------------------------<< Sensors

//------------------------------------------------------------------>> Buttons

if(buttonPress[1]!=lastButtonPress[1]){
  if(buttonPress[1]==HIGH){
    mode++;
  }
}
lastButtonPress[1]=buttonPress[1];


if(buttonPress[2]!=lastButtonPress[2]){
  if(buttonPress[2]==HIGH){
    subMode--;
  }
}
lastButtonPress[2]=buttonPress[2];


if(buttonPress[3]!=lastButtonPress[3]){
  if(buttonPress[3]==HIGH){
    subMode++;
  }
}
lastButtonPress[3]=buttonPress[3];


if(buttonPress[4]!=lastButtonPress[4]){
  if(buttonPress[4]==HIGH){
    subsubMode++;
  }
}
lastButtonPress[4]=buttonPress[4];



for(int x=1; x<5; x++){  
buttonState[x]=digitalRead(buttonPin[x]);

if(buttonState[x]!=lastButtonState[x]){
  lastButtonState[x]=buttonState[x];
  if(buttonState[x]==LOW)buttonPress[x]=1;
  else buttonPress[x]=0;
}

}

//------------------------------------------------------------------<< Buttons



//------------------------------------------------------------------>>
//------------------------------------------------------------------<<
//------------------------------------------------------------------>>
//------------------------------------------------------------------<<
//------------------------------------------------------------------>>
//------------------------------------------------------------------<<

}


//========================================================================================================>>FUNCTIONS

int bPress(int bPin){                                                             //button function
    
}  
  

void lcdPrintText (int x, int y, char* text){
  
  lcd.setCursor(x,y);  
  lcd.print(text);
}  


//------------------------------------------------------------------<<
3  Using Arduino / Programming Questions / Need help/advice in 2x16 LCD menu on: July 27, 2014, 04:23:11 am
I have problem making an iterative lcd menu for my board contains of few sensor, 4 buttons, and a a lcd.  

button 1 - for changing mode (this is easy)
button2-3 - for moving an arrow to change different values, such as setting time and alarm. (also this 2 buttons can be used to change values.)
button4 - is the okay/select button

the problem of my program is, I have no idea how to make it work. It's easy to change from one screen to one screen, but I have no idea how to achieve the functions mentioned above.

any tips, advices?

Mode 1 - mode4


subMode - sub menu for mode 1, for setting time and alarm, and change temp reading from F/C


Full code
Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(2,3,4,5,6,7);

byte timeIcon[8] = {
0b11111,
0b10001,
0b01010,
0b00100,
0b00100,
0b01010,
0b10001,
0b11111};

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

byte batteryIcon[8] = {
0b01110,
0b11011,
0b10001,
0b11111,
0b10001,
0b11111,
0b11111,
0b11111};

byte lightIcon[8] = {
        0b00100,
0b10101,
0b01110,
0b11011,
0b01110,
0b10101,
0b00100,
0b00000};

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

byte buttonIcon[8] = {
0b00000,
0b00000,
0b00000,
0b11111,
0b10001,
0b10101,
0b10001,
0b11111};

byte alarmIcon[8] = {
        0b00000,
0b00100,
0b01110,
0b01110,
0b01110,
0b11111,
0b00100,
0b00000
};

int timeI=0;
int tempI=1;
int batteryI=2;  
int lightI=3;
int arrowupI=4;  
int buttonI=5;  
int alarmI=6;  


#define tempI char(0x01)
#define batteryI char(0x02)  
#define lightI char(0x03)
#define arrowupI char(0x04)  
#define buttonI char(0x05)
#define alarmI char(0x06)
#define timeI char(0x07)
//====================================================================================================================================
//====================================================================================================================================

// Pinouts =====>
int buttonPin[6]={0,A4,A3,A2,A1}; // Button 1-4
int ledPin[7]={0,8,9,10,11,12};   // Led 1-5
int shuntPin=A5;              
int tempPin=A6;
int lightPin=A7;
int beeperPin=A0;


// TimeValues  ======>
int DAY=0; //Day
int HR=0; //hour
int MIN=0; //minute
int SEC=0; //Second
int lastSec=0;
unsigned long previousSec=0;

int bdHR=0; // hour --- timer for battery discharge mode
int bdMIN=0; //minute
int bdSEC=0; //Second

int cdDAY=0; // days --- countdown timer for battery discharge mode
int cdHR=0; // hour
int cdMIN=0; //minute
int cdSEC=0; //Second

//   ======> sensorValues
int shuntMa=0;
int shuntMah=0;
unsigned long shuntMatotal=0;
int tempF=0;
int tempC=0;
int lightLevel=0;

//   ======> menus
int mode=1;
int lastMode=1;
int subMode=0;
int subsubMode=0;
int arrowLoc[16]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//   ======> button
int buttonState[5]={0,0,0,0,0};
int lastButtonState[5]={0,1,1,1,1};
int buttonPress[5]={0,0,0,0,0};
int lastButtonPress[5]={0,0,0,0,0};
//   ======>


//====================================================================================================================================
//====================================================================================================================================

4  Using Arduino / Microcontrollers / Re: questions about tx and rx, usb-serial on: July 27, 2014, 02:55:08 am
1, is TTL same thing as serial?
Not quite - there are serial levels on TTL, and there is the RS-232 standard; the voltage levels are inverted, and different.

2, can I use this usb-ttl's tx and rx pin to upload sketch to my standalone new avr mega8 chip?
Lovely picture!  Yes, it will work fine but that module does not provide for the auto-reset function using a capacitor and 10k resistor (unless you are deft with a soldering iron) so you will have to learn to use the reset button on your board design to start the download - and of course you have to have the bootloader already installed on the mega8 and a suitably adapted "boards.txt" file.

3, I have a UNO. Can I use the above USB-TTL to talk to my UNO? how?
The UNO already has a USB interface.  What exactly is it you want to do?


thanks a lot for the answer. I have already tried using an uno to talk to this USB-ttl. I uploaded a( Serial.print("test")) to my uno, and connect pin Tx in my uno to this USB-ttl adapter's  Rx pin. It reads out fine, but I have another question. Why it's not Tx pin connect to Tx pin? it doesn't work with Tx connected to Tx, Rx to Rx.

also, I got one of those RF, radio frequency mini board. I use it on my uno to talk to my computer using this usb-ttl, but a lot of random code shows up. I think that is noise, right? how to avoid that?
5  Using Arduino / Microcontrollers / questions about tx and rx, usb-serial on: July 26, 2014, 11:13:56 pm
1, is ttl same thing as serial?

2, can I use this usb-ttl's tx and rx pin to upload sketch to my standalone new avr mega8 chip?


3, I have an uno. Can I use the above usb-ttl to talk to my uno? how?
6  Using Arduino / Programming Questions / Re: button function return wrong on: July 26, 2014, 01:26:35 pm
I don't have the full code now. I have rewritten it already. still have problem. the lcd is not displaying correctly.

Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(2,3,4,5,6,7);

byte timeIcon[8] = {
0b11111,
0b10001,
0b01010,
0b00100,
0b00100,
0b01010,
0b10001,
0b11111};

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

byte batteryIcon[8] = {
0b01110,
0b11011,
0b10001,
0b11111,
0b10001,
0b11111,
0b10001,
0b11111};

byte lightIcon[8] = {
0b00000,
0b10101,
0b01110,
0b11011,
0b01010,
0b01110,
0b10101,
0b00000};

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

byte buttonIcon[8] = {
0b00000,
0b11111,
0b10001,
0b10101,
0b10101,
0b10001,
0b11111,
0b00000
};

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

int timeI=0;
int tempI=1;
int batteryI=2;   
int lightI=3;
int arrowupI=4;   
int buttonI=5; 
int alarmI=6; 


#define tempI char(0x01)
#define batteryI char(0x02)   
#define lightI char(0x03)
#define arrowupI char(0x04)   
#define buttonI char(0x05)
#define alarmI char(0x06)
#define timeI char(0x07)
//====================================================================================================================================
//====================================================================================================================================

// Pinouts =====>
int buttonPin[6]={0,A4,A3,A2,A1}; // Button 1-4
int ledPin[7]={0,8,9,10,11,12};   // Led 1-5
int shuntPin=A5;               
int tempPin=A6;
int lightPin=A7;
int beeperPin=A0;


// TimeValues  ======>
int DAY=0; //Day
int HR=0; //hour
int MIN=0; //minute
int SEC=0; //Second
int lastSec=0;
unsigned long previousSec=0;

//   ======> sensorValues
int shuntMa=0;
int shuntMah=0;
unsigned long shuntMatotal=0;
int tempF=0;
int tempC=0;
int lightLevel=0;

//   ======> menus
int mode=1;
//   ======> button
int buttonState[5]={0,0,0,0,0};
int lastButtonState[5]={0,1,1,1,1};
int buttonPress[5]={0,0,0,0,0};
//   ======>


int testVal=0;
//====================================================================================================================================
//====================================================================================================================================

void setup() {
 
  //===5 RED LEDs ===
for(int x=1; x<7; x++){
  pinMode(ledPin[x],OUTPUT);

  //===4 Black buttons === 
for(int x=1; x<5; x++){
  pinMode(buttonPin[x], INPUT_PULLUP);

  //===Sensors ===
  pinMode(shuntPin, INPUT);// shunt voltage   
  pinMode(tempPin, INPUT);// Temp. Sensor 
  pinMode(lightPin, INPUT);// Light resistor   
  pinMode(beeperPin, OUTPUT); // Beeper 
 
  //===LCD SETUP === 
  lcd.begin(16,2);
  lcd.createChar(7, timeIcon);
  lcd.createChar(1, tempIcon);
  lcd.createChar(2, batteryIcon);   
  lcd.createChar(3, lightIcon); 
  lcd.createChar(4, arrowupIcon);   
  lcd.createChar(5, buttonIcon); 
  lcd.createChar(6, alarmIcon); 
 
}

//====================================================================================================================================
//====================================================================================================================================
void loop() {

   
 
//------------------------------------------------------------------>>LCD
if(mode==1){

  char x [16];
  sprintf(x, "%c%02d:%02d:%02d:%02d %02d%c", timeI,DAY,HR,MIN,SEC,tempC,tempI);
  lcdPrintText(0,0,x);
   
  char y [16];
  sprintf(y, " %cM %c+ %c- %cY %003d", buttonI,  buttonI, buttonI, buttonI, testVal );
  lcdPrintText(0,0,y);
 
}

if(mode==2){
lcd.clear();   
}

if(mode==3)mode=1;

/*


  char tempRead[5];
  sprintf(tempRead, "%02d %02d", lightLevel,tempC); 
  lcd.setCursor(10, 0);   
  lcd.print(tempRead);   
 
  char shuntRead[14];
  sprintf(shuntRead,"%04dmA %04dMah",shuntMa, shuntMah);
   lcd.setCursor(1, 1);
  lcd.print(shuntRead);   
 
  */
     
//------------------------------------------------------------------<<LCD


//------------------------------------------------------------------>>Time
unsigned long currentSec=millis();
if(currentSec-previousSec>1000) {
SEC++;
previousSec=currentSec;
if(shuntMa>1){shuntMatotal=shuntMatotal+shuntMa;}
}

if(SEC==60){SEC=0; MIN++;}
if(MIN==60){MIN=0; HR++;}
if(HR==24) {HR=0;DAY++;}
//------------------------------------------------------------------<<Time




//------------------------------------------------------------------>> Sensors
// 8% difference from meter to arduino reading
int shuntVal= analogRead(shuntPin);
shuntMa=shuntVal*(5.0/1023.0*1000.0*0.92);              //shuntMa
shuntMah=shuntMatotal/3600;                            //shuntMah

//(21%) tempVal*0.21=F degree/ (F-32)X5/9=C degree
int tempVal= analogRead(tempPin);
tempF=tempVal*0.21;
tempF= constrain (tempF, 0,99);                      // tempF
tempC=((tempVal*0.21)-32)*5/9;                       // tempC

//darkest 50-1010 lightest
int lightVal= analogRead(lightPin);
lightVal=map(lightVal, 50,1010,0,99);
lightLevel=constrain (lightVal, 0, 99);             // lightLevel

//------------------------------------------------------------------<< Sensors

//------------------------------------------------------------------>> Buttons
if(buttonPress[1]==1) {mode++; }

if(buttonPress[2]==1)digitalWrite(ledPin[1], HIGH);
else digitalWrite(ledPin[1], LOW);

testVal=buttonPress[2];

 
buttonState[1]=digitalRead(buttonPin[1]);

if(buttonState[1]!=lastButtonState[1]){
  if(buttonState[1]==LOW)buttonPress[1]=1;
  else buttonPress[1]=0;
}
  lastButtonState[1]=buttonState[1];
 
 
buttonState[2]=digitalRead(buttonPin[2]);

if(buttonState[2]!=lastButtonState[2]){
  if(buttonState[2]==LOW)buttonPress[2]=1;
  else buttonPress[2]=0;
}
  lastButtonState[2]=buttonState[2]; 


//------------------------------------------------------------------<< Buttons



//------------------------------------------------------------------>>
//------------------------------------------------------------------<<
//------------------------------------------------------------------>>
//------------------------------------------------------------------<<
//------------------------------------------------------------------>>
//------------------------------------------------------------------<<

}


//========================================================================================================>>FUNCTIONS

int bPress(int bPin){                                                             //button function
     

 

void lcdPrintText (int x, int y, char* text){
 
  lcd.setCursor(x,y); 
  lcd.print(text);



//------------------------------------------------------------------<<
7  Using Arduino / Programming Questions / Re: [Solved]how to simplify createChar() custom character written to 2X16 lcd? on: July 26, 2014, 12:34:50 pm
this looks great to me, I will try it now. few questions:

1, why #include <Wire.h>?
(...)

Because the LCD that the poster uses is an I2C LCD. In your case you don't need it.


I don't know how you can say that this:
Code:
   char buffer[16];

  sprintf(buffer, "%c %c %c", char(0x02),char(0x01),char(0x03));
  lcd.print(buffer);

is better than this:
Code:
  char myText[17];
  sprintf(myText, "%c%02d:%02d:%02d %c  PigOS", TIME, HR, MIN, SEC, TIME);
  lcdPrintXYSpecialLine(0, 0, myLine);

It didn't work? You don't understand something of what I did?

it works now, everything and all. thank you so much.
8  Using Arduino / Programming Questions / [half Solved, Still in doubt]button function return wrong on: July 26, 2014, 12:32:15 pm
Code:
int buttonState[5]={0,0,0,0,0};
int lastButtonState[5]={0,1,1,1,1};


Low is high, because I use INPUT_PULLUP here.
Code:
int bPress(int bPin){                                                             //button function
buttonState[bPin]=digitalRead(buttonPin[bPin]);

if(buttonState[bPin]!=lastButtonState[bPin]){
  lastButtonState[bPin]=buttonState[bPin];
  if(buttonState[bPin]==LOW)return 1;
  else return 0;


}      
}  

it won't work properly. changed test to low, test it work half way. my testVal increases very fast. what is wrong here?

Code:
if(bPress(1)==0) {mode++; testVal++;}

if(bPress(2)==0)digitalWrite(ledPin[1], HIGH);
else digitalWrite(ledPin[1], LOW);

if(bPress(3)==0)digitalWrite(ledPin[2], HIGH);
else digitalWrite(ledPin[2], LOW);

if(bPress(4)==0)digitalWrite(ledPin[3], HIGH);
else digitalWrite(ledPin[3], LOW);


+++
testVal return 1 when button 2 is not pressed; return 0 when it it pressed. why???

Code:
testVal=bPress(2);




I don't think this is a smart way to do that, but it works. still don't understand why the previous code return 1 instead of 0.
Code:
//------------------------------------------------------------------>> Buttons

if(buttonPress[1]!=lastButtonPress[1]){
  if(buttonPress[1]==HIGH){
    digitalWrite(ledPin[1], HIGH);
    testVal++;
  }
  else digitalWrite(ledPin[1], LOW);
}
lastButtonPress[1]=buttonPress[1];



for(int x=1; x<5; x++){ 
buttonState[x]=digitalRead(buttonPin[x]);

if(buttonState[x]!=lastButtonState[x]){
  lastButtonState[x]=buttonState[x];
  if(buttonState[x]==LOW)buttonPress[x]=1;
  else buttonPress[x]=0;
}

}

//------------------------------------------------------------------<< Buttons
9  Using Arduino / Programming Questions / Re: press button proceed to main program on: July 26, 2014, 12:06:43 pm
Thanks guys smiley it's working.
If I add another 2 buttons what will I do?

Very last question.  smiley-grin


my project has 4 buttons. here is how I do it.

Code:
int buttonPin[6]={0,A4,A3,A2,A1}; // Button 1-4

Code:
int buttonState[5]={0,0,0,0,0};
int lastButtonState[5]={0,1,1,1,1};


Code:
int bPress(int bPin){                                                             //button function
buttonState[bPin]=digitalRead(buttonPin[bPin]);

if(buttonState[bPin]!=lastButtonState[bPin]){
  lastButtonState[bPin]=buttonState[bPin];
  if(buttonState[bPin]==LOW){
    return 1;}
  else return 0; 
}     

 


Code:
int buttonTest1=bPress(1);
if(buttonTest1==0) mode++;

int buttonTest2=bPress(2);
if(buttonTest2==0)digitalWrite(ledPin[1], HIGH);
else digitalWrite(ledPin[1], LOW);
10  Using Arduino / Programming Questions / Re: press button proceed to main program on: July 26, 2014, 11:15:41 am
http://arduino.cc/en/Tutorial/ButtonStateChange

this should work.

Code:
int timer = 100;       
int ledPins[] = {
  13,12,11, 10, 9, 8, 7, 6};     
int pinCount = 8;       
int button = 0;

long lastbutton=0;   

void setup() {
 
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);     
  }
 
 
  pinMode(button, INPUT);   
}

void loop() {
 
  long currentbutton= digitalRead (button);   
  if(currentbutton!=lastbutton)  {
  if(currentbutton==HIGH){
   
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                 
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);   

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }   
  }
  }

 

}
11  Using Arduino / Programming Questions / Re: how to simplify createChar() custom character written to 2X16 lcd? on: July 26, 2014, 10:56:11 am
luisilva is correct and you can get this working with sprintf. As he says, it is important to not use address 0x00 as the custom characters are numbered 1 through 8. There is also a significant memory cost to using sprintf, and my sketch had about a 1500 byte increase over just calling lcd.print with the formatting.

The following test sketch works for me

Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,4,5,6,0,1,2,3,7,NEGATIVE);//mjkdz board

byte timeIcon[8] = {
0b11111,
0b10001,
0b01010,
0b00100,
0b00100,
0b01010,
0b10001,
0b11111};

byte  bell[8]  = {
  0x4, 0xe, 0xe, 0xe, 0x1f, 0x0, 0x4};

byte  heart[8] = {0x0, 0xa, 0x1f, 0x1f, 0xe, 0x4, 0x0};

void setup() {
 
  lcd.begin(16, 2);
  lcd.createChar(1,bell);
  lcd.createChar(2,heart);
  lcd.createChar(3,timeIcon);
  lcd.clear();
 
  delay(1000);
 
  char buffer[16];

  sprintf(buffer, "%c %c %c", char(0x02),char(0x01),char(0x03));
  lcd.print(buffer);
 
}
 

void loop() {
}

I do not think you can get a simple wrap to the second line with a larger buffer and more characters. The memory structure of the HD44780 lcd controller is not straight forward.  Here's a good reference:

  http://web.alfredstate.edu/weimandn/lcd/lcd_addressing/lcd_addressing_index.html



this looks great to me, I will try it now. few questions:

1, why #include <Wire.h>?
2,  char(0x02) is referring to lcd.createChar(2,heart); ?  (is so, is it possible to use words instead of this 0x02?)
12  Using Arduino / Programming Questions / Re: how to simplify createChar() custom character written to 2X16 lcd? on: July 26, 2014, 10:47:31 am
OK. Now I understand the question. I'm sorry about that.

Maybe something like this:
Code:
lcdPrintXYSpecialLine(byte X, byte Y, char* text) {

  lcd.setCursor(X, Y);
   while (*text) {
      Serial.write(*text++);
   }
}

To create the string to send to the LCD you need to know 1 thing. You CAN NOT record any "icon" in the first position of the memory (address 0x00). So, you need to change this:
Code:
#define TIME 0

Then, you can do:
Code:
 char myText[17];
  sprintf(myText, "%c%02d:%02d:%02d %c  PigOS", TIME, HR, MIN, SEC, TIME);
  lcdPrintXYSpecialLine(0, 0, myLine);

  sprintf(myText, "%cMode %c+ %c- %cOK", BUTTON, BUTTON, BUTTON, BUTTON);
  lcdPrintXYSpecialLine(0, 1, myLine);

One more time I didn't try it, so be careful.

thank you so much.
13  Using Arduino / Installation & Troubleshooting / Re: Questions regarding bootloader, avr chip, Standalone arduino and RX and TX on: July 26, 2014, 10:41:48 am
1. You can buy ATmega328p chips with bootloader. But since you don't know if that bootloader is good, you should burn the bootloader anyway.

2. Yes. On the Arduino Uno it is called the ICSP header. You can see it in the schematics.
http://arduino.cc/en/Main/arduinoBoardUno

3. I don't understand. You can burn the bootloader by placing an ATmega328P chip in the Uno and use a programmer and the ICSP header. After that you can use RX and TX.
However, you can also upload a sketch without bootloader. You can do that only with a programmer and the ICSP header, and it is somewhere in the Arduino IDE menu to upload without bootloader.
You can also use the Arduino Uno as a programmer, called "Arduino as ISP". So you can use the Arduino Uno to burn a bootloader in another ATmega chip.
http://arduino.cc/en/Tutorial/ArduinoISP

4. An ATmega8 chip running at 16MHz ? Then is it is compatible with the Arduino ATmega8 NG.
You can place the ATmega8 in the Uno and use a programmer and ICSP header to burn the ATmega8 NG bootloader.
The ATmega8 will work in the Arduino Uno (I have never done that, but I think it will work).


okay, thank you a lot. I think I got it.
14  Using Arduino / Installation & Troubleshooting / [Solv]Questions regarding bootloader, avr chip, Standalone arduino and RX and TX on: July 26, 2014, 10:05:02 am
1, do I have to burn the bootloader to a new avr chip in order to use arduino IDE to program it?

2, I think I can use pins 10, 11, 12, and 13 in my uno as an ISP to program a new avr chip. Am I correct?

3, I have to burn a bootloader to a new sthandalone avr chip, for uploading sketch using RX and TX pin of my uno. am I correct? and I need to remove the chip from my uno to do so?

4, I have a new Amega8 chip. can I use it in my uno board? it is the same size. if yes, do I just burn the bootloader to this chip and put it into my uno, and it will work?

thank you
15  Using Arduino / Programming Questions / Re: Button Not always working with changing LED's. on: July 25, 2014, 09:33:42 pm
for the

Code:
lastButtonState[4]=buttonState[4];


if nothing happen to the buttonState, lastButtonState is alway equals to lastButtonState. if something happened to ButtonState, then lastButtonState equal to ButtonState. lastButtonState here is for detection. you can put it anywhere in the code.
Pages: [1] 2 3 ... 10