void setup() { Wire.begin(); if (debugSerial){ Serial.begin(115200); Serial.println(F("=================== SETUP =================")); } // initialize device if (debugSerial && debugMPU6050) Serial.println(F("Initializing I2C devices...")); accelgyro.initialize(); // verify connection if (debugSerial && debugMPU6050) { Serial.println("Testing device connections..."); boolean OK = accelgyro.testConnection() ; ( OK )? Serial.println(F("MPU6050 connection successful")): Serial.println(F("MPU6050 connection failed")); } if (debugSerial){ Serial.println(F("=============== FIM SETUP =================")); } }
/*** * Read [pin1] [pin2] ... * * Read pin values * Pins are given in the following way: A0 A1 ... for analog pins * D0 D1 ... for digital pins * Answer is: val1 val2 ... */ void cmdRead(int argC, char **argV) { char pinType[2]; int pin; int value; for (int i = 1; i <= argC; i++) { pinType[0] = argV[i][0]; pinType[1] = NULL; pin = strtol(&(argV[i][1]), NULL, 10); if (strcasecmp(pinType, "D") == 0) { value = digitalRead(pin); } else if (strcasecmp(pinType, "A") == 0) { value = analogRead(pin); } else { return; } // Add read values to answer string Serial.print(value); if (i < argC) { Serial.print(' '); } } }
//! Test function to validate that the fifo works as expected. //! It is always a good idea to validate your data structures. Languages like //! Python or Perl have such validation built in, while C++ does not. This function //! can be used in a special build to run through some test cases and make sure //! that the fifo behaves as we exect, especially in the edge cases, such as when //! it gets full, wraps around, etc. void FifoTest(HardwareSerial& ds) { Fifo f1(20); Fifo::FifoType x; char buffer[128]; for (x=1;x<25;x++) { f1.push(&x); sprintf(buffer,"Fifo push %d count %d\n",x,f1.count()); ds.print(buffer); } f1.clear(); sprintf(buffer,"Fifo count %d\n",f1.count()); ds.print(buffer); for (x=0;x<10;x++) { f1.push(&x); } sprintf(buffer,"Fifo count %d. Expected 10\n",f1.count()); ds.print(buffer); Fifo::FifoType y; for (x=1;x<15;x++) { f1.pop(&y); sprintf(buffer,"Fifo pop %d count %d\n",y,f1.count()); ds.print(buffer); } }
void setup() { // Set up serial port for debugging Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // Fullfil the data array createDataArray(); // Read and set the key from the EEPROM readKey(KEYLENGTH); // Inicialization of the key time = micros(); // time start aes192_init(key, &key_init); emit = micros(); // time start Serial.print(F("Inicialisation total takes: ")); Serial.print(emit - time); Serial.println(F(" [us]")); // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(ledPin, OUTPUT); }
File create_gps_file() { int year; unsigned long age; byte month, day, hour, minute, second, hundredth; gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredth, &age); String filename; filename.concat(GPS_DIR); filename.concat('/'); filename.concat(year); filename.concat('/'); filename.concat(month); filename.concat('/'); filename.concat(day); filename.concat('/'); filename.concat(hour); filename.concat(minute); filename.concat(second); filename.concat(".GPS"); char buf[filename.length() + 1]; filename.toCharArray(buf, sizeof(buf)); Serial.print("Criando arquivo... "); Serial.println(buf); return create_file(buf); }
void setup() { Serial.begin(115200); nss.begin(9600); pinMode(10, OUTPUT); pinMode(GREEN_LED, OUTPUT); pinMode(RED_LED, OUTPUT); Serial.println("Iniciando..."); if (!SD.begin()) Serial.println("SD Erro!"); }
/*** * SerSend [hard | soft] [port] * * After this command, each character sent is mirrored to the chosen serial * port until the NULL character (0x00) is sent (also mirrored) */ void cmdSerSend(char **argV) { boolean isSoftSerial = (strcasecmp(argV[1], "soft") == 0); int currPort = strtol(argV[2], NULL, 10); Serial.println(doneString); if (currPort < 1 || currPort > ((isSoftSerial)? SOFT_SER_MAX_PORTS : HARD_SER_MAX_PORTS)) { return; } if (isSoftSerial) { #ifdef USE_SOFTWARE_SERIAL softSerDescs[currPort-1].txMsgLen = 0; #endif } // mirror the hardware serial and the software serial while (true) { if (Serial.available()) { char c = Serial.read(); if (isSoftSerial) { #ifdef USE_SOFTWARE_SERIAL softSerDescs[currPort-1].txMsg[softSerDescs[currPort-1].txMsgLen++] = c; #endif } else { #if HARD_SER_MAX_PORTS > 0 hardSerHandler[currPort-1]->write(c); #else return; #endif } if (c == '\0') { // acknowledge Serial.println(doneString); delay(10); #ifdef USE_SOFTWARE_SERIAL if (isSoftSerial) { // send the message, and remember the answer for (int i = 0; i < softSerDescs[currPort-1].txMsgLen; i++) { softSerDescs[currPort-1].handler->write(softSerDescs[currPort-1].txMsg[i]); } softSerDescs[currPort-1].rxMsgLen = 0; while (!Serial.available()) { if (softSerDescs[currPort-1].handler->available() && softSerDescs[currPort-1].rxMsgLen < SOFT_SER_MSG_SIZE) { softSerDescs[currPort-1].rxMsg[softSerDescs[currPort-1].rxMsgLen++] = softSerDescs[currPort-1].handler->read(); } } } #endif return; } } } }
void loop2() { vw_wait_rx(); if (vw_get_message(buf, &buflen)) // Non-blocking { int port=parse(); switchState(port); Serial.print(port); Serial.println(); } }
// get next byte from serial stream byte Comm::serial_get(bool block) { if (block) { while (true) { if (Serial.available() > 0) { break; } } } return Serial.read(); }
void Node::ajuste_pinos(){ if (sizeof(this->comandos) > 0 ){ if(this->_debug) Serial.println("Ajustando pinagem: "); for (int i = 0; i < sizeof(this->comandos)-1; i++){ pinMode(this->comandos[i].pino, OUTPUT); if(this->_debug) Serial.print(this->comandos[i].pino+" "); }; if(this->_debug) Serial.println("Fim de Ajuste."); } }
void setup() { Serial.begin(9600); Serial.println("setup"); relays[0] = createRelay(PORT_0); relays[1] = createRelay(PORT_1); relays[2] = createRelay(PORT_2); relays[3] = createRelay(PORT_3); vw_set_rx_pin(PORT_RF); vw_setup(4000); // Bits per sec vw_rx_start(); // Start the receiver PLL running }
int calcDeltaT(){ lastLoopUsefulTime = millis()-loopStartTime; if(lastLoopUsefulTime<STD_LOOP_TIME) { delay(STD_LOOP_TIME-lastLoopUsefulTime); if (debugSerial && debugLoopTime){ Serial.print(" Esperei: "); Serial.println(STD_LOOP_TIME-lastLoopUsefulTime); } } lastLoopTime = millis() - loopStartTime; loopStartTime = millis(); return lastLoopTime; }
void serialEventRun(void) { #ifdef serialEvent_implemented if (Serial.available()) serialEvent(); #endif #ifdef serialEvent1_implemented if (Serial1.available()) serialEvent1(); #endif #ifdef serialEvent2_implemented if (Serial2.available()) serialEvent2(); #endif #ifdef serialEvent3_implemented if (Serial3.available()) serialEvent3(); #endif }
template<class T> int writeObject(HardwareSerial ser, T& value, char name) { unsigned char* p = (unsigned char*) (void*) &value; unsigned int i; char checksum = '0'; ser.write('@'); ser.write(name); for (i = 0; i < sizeof(value); i++) { ser.write(*p++); checksum = checksum ^ *p; } ser.write(checksum); ser.write('\n'); return i; }
void loop() { bool newData = false; unsigned long start = millis(); while (millis() - start < 1000) { while (nss.available()) { if (gps.encode(nss.read())) newData = true; } } if (newData) { if (gps.hdop() < MAX_HDOP) signal_fixed_status(); else signal_instable_status(); if (!gps_filename) { gps_filename = create_gps_file(); } String line = create_gps_line(); gps_filename.println(line); gps_filename.flush(); Serial.println(line); } else { signal_unavailable_status(); } }
void GetDataSpace::execute (HardwareSerial& stream, McuState&) const { unsigned char* ptr = (unsigned char*)address; unsigned char value = *ptr; char response[MAX_NUMBER_LENGTH]; Utils::toDecimal (response, sizeof (response), value); stream.println (response); }
Stream* digitalPinToSerial(int pin, unsigned long baud){ #if defined(__AVR_ATmega328P__) return NULL; #elif defined(__AVR_ATmega2560__) if(pin==19 || pin==18){ Serial1.begin(baud); return &Serial1; } if(pin==17 || pin==16){ Serial2.begin(baud); return &Serial2; } if(pin==15 || pin==14){ Serial3.begin(baud); return &Serial3; } return NULL; #elif defined(__AVR_AT90USB1286__) if(pin==2 || pin==3){ UART.begin(baud); return &UART; } return NULL; #endif }
//call like : serialRead (Serial1, buffer, 12, 5) uint8_t serialRead (HardwareSerial theSerial, uint8_t *buf, uint8_t leng, uint8_t timeout) { int sub; if (theSerial.available ()) { for (sub=0; sub<leng; sub++) { uint32_t start_time = millis (); while (!theSerial.available ()) { if (millis () - start_time > timeout) return sub; } buf[sub] = theSerial.read (); } return sub; } return 0; }
void loop() { if (debugSerial && debugLoopTime) Serial.println(lastLoopTime); atualizaSensors(deltaT); // *********************** loop timing control ************************** deltaT = calcDeltaT(); }
void createDataArray() { Serial.println(F("Creating array of data")); time = micros(); // time start int i, j; for (i = j = 0; i < DATALENGTH; i++, j++) { if (j <= 25) plain[i] = (65 + j); else { plain[i] = char('-'); plain[i + 1] = 65; i++; j = 0; } } // emit = micros(); // time end // // Serial.print(F("Total takes: ")); // Serial.print(emit - time); // Serial.println(F(" [us]")); // // if (beVerbose) // { // Serial.println(F("Printing array data:")); // for (int i = 0; i < DATALENGTH; i++) // Serial.print(plain[i]); // Serial.println(); // } }
/*** * The setup function is called once at startup of the sketch */ void setup() { Serial.begin(SERIAL0_BAUD); pMsg = msg; #ifdef USE_WIRE // Connect to the I2C bus Wire.begin(); #endif // Init hardware serial ports if they exist for (int i = 0; i < HARD_SER_MAX_PORTS; i++) { switch (i + 1) { #if HARD_SER_MAX_PORTS >= 1 case 1: hardSerHandler[i] = &Serial1; break; #endif #if HARD_SER_MAX_PORTS >= 2 case 2: hardSerHandler[i] = &Serial2; break; #endif #if HARD_SER_MAX_PORTS >= 3 case 3: hardSerHandler[i] = &Serial3; break; #endif } } }
template<class T> int readObject(HardwareSerial ser, T& value, char name) { unsigned char* p = (unsigned char*) (void*) &value; unsigned int i; char checksum = '0'; for (i = 0; i < sizeof(value); i++) { *p++ = ser.read(); checksum = checksum ^ *p; } //TODO: make sure this actually works if (ser.read() == checksum) { Serial.print("Checksum valid"); } else { Serial.print("Checksum invalid"); } return i; }
void OpenDeviceClass::begin(HardwareSerial &serial, unsigned long baud){ serial.begin(baud); DeviceConnection *conn = new DeviceConnection(serial); begin(*conn); }
bool RTArduLinkHALAddHardwarePort(RTARDULINKHAL_PORT *port, long portSpeed, unsigned char hardwarePort) { HardwareSerial *hardPort; switch (hardwarePort) { case 0: #if defined(USBCON) /* Leonardo support */ hardPort = &Serial1; #else hardPort = &Serial; #endif break; case 1: #if defined(UBRR1H) hardPort = &Serial1; #else return false; #endif break; case 2: #if defined(UBRR2H) hardPort = &Serial2; #else return false; #endif break; case 3: #if defined(UBRR3H) hardPort = &Serial3; #else return false; #endif break; default: return false; } port->serialPort = hardPort; hardPort->begin(portSpeed); // start the port return true; }
void loop() { if (Dennao.available() > 0) { read_buf_len = Dennao.recv(read_buf, DENNAO_RX_SIZE, 0); for(int i=0;i<read_buf_len;i++){ Uart.print((char)read_buf[i]); } } if (Uart.available() > 0) { uint8_t incomingByte = Uart.read(); if(incomingByte=='\r'){ Dennao.send(out_buf, out_buf_len, 0); }else{ out_buf[out_buf_len++] = incomingByte; } } }
bool GetStack::handleExcessiveRequest (HardwareSerial& stream, McuState& state) const { if (frameCount <= state.stackSize) {return false;} char buf[COMMAND_BUF_SIZE]; char* p = Utils::strcpy (buf, COMMAND_BUF_SIZE, "ERR: GS"); Utils::toDecimal(p, COMMAND_BUF_SIZE - 7, frameCount); stream.println (buf); return true; }
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle) { error_count++; /* Reset the UART. The begin() method only takes effect if */ /* the baud rate changes, so force a change here. */ uint32_t baud = huart6.Init.BaudRate; huart6.Init.BaudRate = 0; Serial6.begin(baud); }
void Node::executa(String mensagem){ //serializa // mensagem "C-S0-01-1" String comando[] = {mensagem.substring(0,3),mensagem.substring(5,6), mensagem.substring(8,8)}; if(this->id == comando[0]){ for(int i=0; i < sizeof(this->comandos) +1 ;i++){ if (this->comandos[i].id_objeto == comando[1]){ if(this->comandos[i].seq_ir_code == NULL){ // executa ação digitalWrite(this->comandos[i].pino,this->comandos[i].acao); if(_debug) Serial.println("enviar código infra red para o pino "+this->comandos[i].pino); }else if(_debug) Serial.println("enviar código infra red para o pino "+this->comandos[i].pino); } } }else if(_debug) Serial.println("Node::executa - comando não é para este node /n Node: "+id+"/n Destinatario: "+comando[0] ); }
void setup() { Serial.begin(9600); // Affectation des servos pour chaque pattes for(int i = 0; i < 4; i++){ servos[i].attach(servoPins[i]); } }
void Utils::readUntil (HardwareSerial& stream, char terminator, char* buf, int bufLen) { int idx = 0; while (idx < bufLen - 1) { int c = stream.read (); if (c == terminator) {break;} if (c < 0) {continue;} buf[idx++] = c; } buf[idx++] = 0; }