วันพฤหัสบดีที่ 30 มีนาคม พ.ศ. 2560

arduino knowledge technic C++

/*--------------
somevar = (expression) ? value1 : value2;

/**************



  for (i = 0; i < sizeof(myStr) - 1; i++){
    Serial.print(i, DEC);
    Serial.print(" = ");
    Serial.write(myStr[i]);
    Serial.println();
  }
  char myInts[]= "124";

  for (i = 0; i < strlen(myInts); i++) {
  // do something with myInts[i]

      Serial.print(i, DEC);
    Serial.print(" = ");
    Serial.write(myInts[i]);
    Serial.println();

**หาความยาวสตริง  จาก char[] ใช้อันล่างจะได้ขนาดที่ถูกต้อง

void ShowMsgDialog(char mystr[],byte textS) {
  int text_w = 0;
  int text_h = 0;
  text_h = 8 * textS;
  String tstr=  mystr;
  text_w = tstr.length() * (6*textS);   //บรรทัดนี้
  int center_w = (display.width()/2) - (text_w/2);
  int center_h = (display.height()/2)- (text_h/2); ;
  display.setTextSize(textS);
  display.clearDisplay(); // clears the screen and buffer
  delay(10);
  display.setCursor(center_w, center_h);
  display.print(mystr);
  display.display();
  delay(2000);
}


/*******************
//*****อีกแบบ   Serial.print(char(b));

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. For example:
  • Serial.print(78) gives "78"
  • Serial.print(1.23456) gives "1.23"
  • Serial.print('N') gives "N"
  • Serial.print("Hello world.") gives "Hello world."

Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms as Serial.print().


You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :
  • Serial.print(F(“Hello World”))
///*****************
ข้อมูลที่เก็บใน eeprom ถ้าว่างไม่มีอะไร ให้ตรวจสอบด้วย isnan
OK, figured it out. The number is being pulled from the EEPROM, so on first load there is no number stored yet. Previous Arduino versions just displayed a 0.0 in the serial monitor, but now it displays a nan.

I tried testing it for a NULL but that don't work, so how do I test for a nan? I want to set a default value if there is nothing in EEPROM yet.

EDIT
Got it!

if(isnan(VALUE)


//สร้างตัวแปรประเภท byte เก็บค่าตัวเลข นำไปเทียบกับ int ได้ ซึ่งจะเป็นตัวเลขที่จัดเก็บ

/**************
As AWOL indicated, the stdlib (i think it is stdlib) linked in by Arduino does not support floats in the sprintf.
One way around this would be to tinker with Arduino IDE to include the full/extended stdlib to get the float working in sprintf.
Another way is to convert the float using :
    dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);

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

  void loop() {
    float F = 1.25;
    char buffer[16];
  
    dtostrf(F,5,2,buffer);
    Serial.print("Float : ");
    Serial.println(buffer);
  
    //Do not loop
    while(true) {};
  }

/*************************
array2มิติ
<datatype> array [DIM_0_SIZE] [DIM_1_SIZE] = {
  //as many vals as dim1
 {val,val,val},
 {val,val,val}//as many rows as dim0
};


///ตัวอย่าง

byte sensors[2][8] = {
 {0x10, 0x18, 0xc3, 0x33, 0x02, 0x08, 0x00, 0x94},
 {0x10, 0x61, 0xef, 0x33, 0x02, 0x08, 0x00, 0x43}
};

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

void loop()
{
   for (int i = 0; i < 2; i++) {
       Serial.print("The bytes of sensor[");
       Serial.print(i);
       Serial.println("]:");
       printArray(sensors[i], 8);
       Serial.println();
   }
   Serial.println();
   delay(10000);
}

void printArray(byte x[], int siz)
{
   char buffer[10];
   for (int i = 0; i < siz; i++) {
       sprintf(buffer, "  0x%02X", x[i]); // Convert hex byte to ascii 0Xvv
       Serial.print(buffer);
   }
   Serial.println();
}




//ง่ายกว่า

***void printArray(byte x[], int siz)

{

for (int i = 0; i < siz; i++) {

Serial.print (" 0x");

Serial.print (x[i], HEX); // if you need leading zeroes put "if (x[i]<16) Serial.print ("0");" before this line

}

Serial.println();

}


//อีกตัวอย่าง






void printArray(byte x[], int siz)




{


for (int i = 0; i < siz; i++) {


Serial.print (" ");


if(x[i] == 51){


Serial.print("num3 ");


char test = (char)x[i];


Serial.print("test " + (String)test);


}


Serial.print ( (char) x[i]); // if you need leading zeroes put "if (x[i]<16) Serial.print ("0");" before this line


}


Serial.println();



//อีกตัวอย่าง array2d
void loop()
{
    printArray(sensors);   //ส่งตัวแปร array ใน ฟังกชั้น
}

void printArray(byte x[][8])
{
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 8; j++) {
      Serial.print (" ");
      Serial.print (x[i][j], HEX); // if you need leading zeroes put "if (x[i]<16) Serial.print ("0");" before this line
    }
  }
  Serial.println();
}


//อีกตัวอย่าง
#define ARRAY_SIZE 3
#define INDEX(x,y) ((x*ARRAY_SIZE)+y)

int *a;

int x[2][3] = {
 {1,2,3},
 {4,5,6}
};
           
void setup(){
 Serial.begin(9600);
 a = &x[0][0];
 Serial.println("x[1][1] val:");
 Serial.println(x[1][1]);
 a[INDEX(1,1)] = 3;
 Serial.println("x[1][1] val post mod:");
 Serial.println(x[1][1]);
}
void loop(){}


/***/อีกตัวอย่าง  กำหนด pointer ก่อน อ้างอิงแบบ array[x*array size + y] เช่น *a ตาม  a = &myAlarm[0][0];

int myAlarm[2][2];
int *a;
byte rom1, rom2;
void setup() {
  //read from eeprom
  Serial.begin(57600);
  rom1 = 04;
  rom2 = 35;
  myAlarm[0][0] = (int)rom1;
  myAlarm[0][1] = rom2 ;
  myAlarm[1][0] = 4;
  myAlarm[1][1] = 32;
  a = &myAlarm[0][0];
  // RTC.setAlarm1Simple(a[0], a[1]);
}

void loop() {
  // put your main code here, to run repeatedly:
  // if (RTC.checkIfAlarm(1)) {
  Serial.println(a[0]);
  Serial.println();
  Serial.println(a[1]);
  Serial.println("rom1:");
  Serial.println(rom1);
  Serial.println("rom2:");
  Serial.println(rom2);

  // RTC.setAlarm1Simple(a[2], a[3]);
  delay(3000);
}

*//////*------------------------------

char sbuf[30];

void show_datetime() {

sprintf( sbuf, "\"%s/%s/%s ","1", "1", "1" );

Serial.print( sbuf );




}


*----------------------------------------

http://users.physik.fu-berlin.de/~goerz/blog/wp-content/uploads/2008/09/ascii.gif




http://arduino.cc/en/Reference/Char




Basically a 5 recieved over serial is in reality a char '5' and the equvalent of the numeric value 53 and a '0' is 48, so the code




byte recieved = Serial.read() - '0'; //recieved will be equal to 53 - 48 == 5 when recieving a '5' on the serial


Here is another way of saying the same thing.

The ASCII codes for the digits 0 through 9 is 48 through 57. When the character '0' is received it actually has a value of 48. The character '1' has the value 49 etc. You can verify this if you add the following to your sketch:




Serial.print("I received: ");

Serial.print(SerialSpeed1, BYTE);

Serial.print(" this has an ASCII value of ");

Serial.print(SerialSpeed1,DEC);




So to convert from the character '0' (which has a value of 48) to its numeric value you need to subtract '0' which is the same as 48.

*-----------------------------------------

DecToBcd



byte decToBcd(byte val)

{

  return( (val/10*16) + (val%10) );    // จากสูตรนี้  จะทำ 25 base10 ไปเป็น  byte 37 bcd(=100101 -->แปลงต่อ 0010 =2 , 0101 = 5 ก็คือ 25 base นั่นเอง)

}

// Convert binary coded decimal to normal decimal numbers

byte bcdToDec(byte val)

{

  return( (val/16*10) + (val%16) );

}




****เสริม

Serial.begin(9600);

byte x,x1;

x=decToBcd(25);

x1=bcdToDec(B00100101);



Serial.println(x,BIN); Serial.print(x1);

**อีกตัวอย่าง
// convert BCD to Decimal
uint8_t DS3231::bcd2dec( uint8_t bcd ) {
  return 10*((bcd >> 4) & 0x0f) + (bcd & 0x0f);
}

// convert Decimal to BCD
uint8_t DS3231::dec2bcd( uint8_t dec ) {
  return ((dec/10) << 4) | (dec%10);

}

/****************************************

  byte b = 0x21;

  printHEX(b);
  printHEX(0xaf);



void printHEX(byte b) {
  char A[17] = "0123456789ABCDEF";
  byte v = b / 16;
  byte w = b % 16;
  Serial.print(A[v]);
  Serial.println(A[w]);
}
/*******************************************

void Say(char x[]){
  Serial.println(x);
}

void Say(float x){
  Serial.println(x);
}

void Say(char x){
  Serial.println(x);
}

void Say(int x){
  Serial.println(x);
}
/*---------------------------------------------------------*
so,
int x = 300;
int *Y = &x;
*Y = 200; // now x = 200
cout << x; // prints 200
Instead now I use a reference ด้านบน  ใช้ไม่ได้ ส่วนอันล่าง ok
  Serial.begin(9600);
  int x=300;
  int& y = x;

  y=200;
 Serial.print(x);
int x = 300;
int& Y = x;
Y = 200; // now x = 200
cout << x; // prints 200

In this context, & is not an operator. It is part of the type.
For any given type T, the type T& is a "reference to T".
The symbol & in fact has three meanings in C++, and it's important to recognise those different meanings.
  • "address of" when applied to an expression
  • "reference" when part of a type
  • "bitwise AND" when applied to two numbers

/*----------------------------------------------------------*
//ดู avr เข้า sleep mode


// Date, Time and Alarm functions using a DS3231 RTC connected via I2C and Wire lib

#include <Wire.h>
#include <SPI.h>   // not used here, but needed to prevent a RTClib compile error
#include <avr/sleep.h>
#include <RTClib.h>


RTC_DS3231 RTC;
int INTERRUPT_PIN = 2;
volatile int state = LOW;

void setup () {
  pinMode(INTERRUPT_PIN, INPUT);
  //pull up the interrupt pin
  digitalWrite(INTERRUPT_PIN, HIGH);

  Serial.begin(57600);
  Wire.begin();
  RTC.begin();
 
  RTC.adjust(DateTime(__DATE__, __TIME__));
  DateTime now = RTC.now();
  RTC.setAlarm1Simple(22, 11);
  RTC.setAlarm2Simple(11, 10);
  RTC.turnOnAlarm(1);
  RTC.turnOnAlarm(2);
  if (RTC.checkAlarmEnabled(1) && RTC.checkAlarmEnabled(2)) {
    Serial.println("Alarms Enabled");
  }
  attachInterrupt(0, logData, LOW);
}



void loop () {
  DateTime now = RTC.now();
  if (RTC.checkIfAlarm(1) || RTC.checkIfAlarm(2)) {
    Serial.println("Alarm Triggered");
  }

  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  Serial.println("Going to Sleep");
  delay(600);
  sleepNow();
  Serial.println("AWAKE");
}

void sleepNow() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  attachInterrupt(0,logData, LOW);
  sleep_mode();
  //HERE AFTER WAKING UP
  sleep_disable();
  detachInterrupt(0);
}

void logData() {
  //do something quick, flip a flag, and handle in loop();
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น