Hi, the problem is that don’t see any data from analog pin in Serial when i have no any data in Serial1.
My Arduino Leonardo connected to car lin-bus through Serial(1) (RX pin) also i have used analog(0) as voltmeter and all data from analog(0) i send to Serial(0). When car switched on and i have data in Serial1 everything works fine, but when i switch off car we have no data in Serial1 and this ok, but also i dont see any data for Serial which reading analog, where can be problem? My code:
#include <HID-Project.h>
#include <HID-Settings.h>
#define linspeed 9600
unsigned long Tbit = 1000000/linspeed;
#define uartlenght 10
//Tbits Header
#define breakf 14
#define longpress 10
int frameerrordelay = ((breakf) - uartlenght)*Tbit;
#define numbers 8
byte message[numbers];
int standByPressCount = 0;
float vPow = 4.7;
float r1 = 100000;
float r2 = 10000;
int timer = 0;
int txData = 0;
void setup() {
UDCON |= (1<<DETACH);
delay(10000);
UDCON &= ~(1<<DETACH);
Keyboard.begin();
Consumer.begin();
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
float v = (analogRead(0) * vPow) / 1024.0;
float v2 = v / (r2 / (r1 + r2));
int floatRound = round(v2); // round current voltage
if (timer < 10000 || floatRound < 52)
txData = 1;
if (floatRound < 52){
timer =0;
}
if (floatRound == 52){
delay(1);
timer+=1;
if (timer == 10000){
txData = 0;
}
}
}
Serial.println(txData); // When Serial1 not availble this line not working, i dont see any data in Serial
if (txData > 0){
LinReading();
if (standByPressCount > 0) {
Keyboard.write(KEY_LEFT_ALT);
delay(10);
standByPressCount = 0;
}
}
if (txData == 0 && standByPressCount == 0)
{
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('d');
delay(100);
Keyboard.releaseAll(); aeg = 0;delay (10);
standByPressCount = 1;
}
}
void LinReading() {
LinRead();
}
void LinRead() {
if (1<<FE1) {
delayMicroseconds(frameerrordelay); //after Frame Error Tbit to Sync Field
for (uint8_t i=0;i<4;i++){
while(!Serial1.available()) { delayMicroseconds(1); }
message[i] = Serial1.read();
}
}
Serial.println(txData); // When Serial1 not availble this line not working, i dont see any data in Serial
In the code you posted this line, and many more, are not in a function. Have you posted the correct code ?
Here is my full code, i don’t think that part with key definition will be interesting for you:
#include <HID-Project.h>
#include <HID-Settings.h>
//Lin Initailisation
#define linspeed 9600
unsigned long Tbit = 1000000 / linspeed;
#define uartlenght 10
//Tbits Header
#define breakf 14
#define longpress 10
int frameerrordelay = ((breakf) - uartlenght) * Tbit;
#define numbers 8
byte message[numbers];
byte sync, PID, checksum;
byte texit, texit_1, enter, enter_1, navil, navir, naviu, navid, navib1_1, navib1, navib2, volu, vold, next, prev, brightness;
byte navib2_1, prev_1, next_1, navil_1, navir_1, naviu_1, navid_1;
int aeg = 0;
int switchApp = 0;
char ctrlKey = KEY_LEFT_GUI;
int klahv = 1;
int standByPressCount = 0;
float vPow = 4.7;
float r1 = 100000;
float r2 = 10000;
int timer = 0;
int txData = 0;
void setup() {
UDCON |= (1 << DETACH);
delay(10000);
UDCON &= ~(1 << DETACH);
Keyboard.begin();
Consumer.begin();
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
voltRead();
Serial.println(txData);
if (txData > 0) {
LinReading();
if (standByPressCount > 0) {
Keyboard.write(KEY_LEFT_ALT);
delay(10);
standByPressCount = 0;
}
}
if (txData == 0 && standByPressCount == 0)
{
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('d');
delay(100);
Keyboard.releaseAll(); aeg = 0; delay (10);
standByPressCount = 1;
}
}
void LinReading() {
LinRead();
PrintlnLin();
ClearFrame();
}
void voltRead() {
float v = (analogRead(0) * vPow) / 1024.0;
float v2 = v / (r2 / (r1 + r2));
int floatRound = round(v2);
if (timer < 10000 || floatRound < 52) {
txData = 1;
if (floatRound < 52)
{
timer = 0;
}
}
if (floatRound == 52) {
delay(1);
timer += 1;
if (timer == 10000) {
txData = 0;
}
}
}
void LinRead() {
if (1 << FE1) {
delayMicroseconds(frameerrordelay); //after Frame Error Tbit to Sync Field
while (!Serial1.available()) {
delayMicroseconds(1);
}
if (Serial1.available()) sync = Serial1.read();
if (sync == 85) {
while (!Serial1.available()) {
delayMicroseconds(1);
}
if (Serial1.available()) {
PID = Serial1.read();
}
if (PID == 32) {
//
for (uint8_t i = 0; i < 4; i++)
{
while (!Serial1.available()) {
delayMicroseconds(1);
}
message[i] = Serial1.read();
}
}
while (!Serial1.available()) {
delayMicroseconds(1);
}
if (Serial1.available())
{
checksum = Serial1.read();
}
}
}
}
// I DONT THINK THAT NEXT CODE WILL BE INTERESTING FOR DEBUGGING :)
//keyboard action definitions - here you define any key
void Next_1() {
Consumer.write(MEDIA_FAST_FORWARD); //source key
aeg = 0;
delay (10);
}
void Menu() {
Keyboard.write(KEY_F2); // menu button
aeg = 0;
delay (10);
}
void Navib2_1() {
Keyboard.write(KEY_F3);
aeg = 0;
delay (10);
}
void Endcall() {
Keyboard.write(KEY_ESC); // End call or return to home
texit = 0;
delay (10);
Keyboard.releaseAll();
aeg = 0;
delay (10);
}
void Call() {
Keyboard.write(KEY_RETURN); // Call voice command
enter = 0;
delay (10);
Keyboard.releaseAll();
aeg = 0;
delay (10);
}
void Navib1_1() {
Keyboard.write(KEY_F6);
aeg = 0;
delay (10);
}
void Next() {
Consumer.write(MEDIA_NEXT); // next track
next = 0;
delay (10);
}
void Prev() {
Consumer.write(MEDIA_PREVIOUS); // prev track
prev = 0;
delay (10);
}
void Prev_1() {
Consumer.write(MEDIA_REWIND); //source key
aeg = 0;
delay (10);
}
void Exit() {
Keyboard.write(KEY_F10); //source key
texit = 0;
delay (10);
}
void Navib2() {
Keyboard.write(KEY_RETURN); //enter button
navib2 = 0;
delay (10);
}
void Navib1() {
Keyboard.write(KEY_ESC); //enter button
navib1 = 0;
delay (10);
}
void Navir() {
if (switchApp == 1) {
Keyboard.write(KEY_TAB);
} else {
Keyboard.write(KEY_RIGHT_ARROW);
} navir = 0;
delay (10);
}
void Navil() {
Keyboard.write(KEY_LEFT_ARROW);
navil = 0;
delay (10);
}
void Naviu() {
Keyboard.write(KEY_UP_ARROW);
naviu = 0;
delay (10);
}
void Navid() {
Keyboard.write(KEY_DOWN_ARROW);
navid = 0;
delay (10);
}
void Navir_1() {
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_TAB);
delay(100);
Keyboard.write(KEY_TAB); switchApp = 1;
}
void Navil_1() {
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('c');
delay(100);
Keyboard.releaseAll(); aeg = 0; delay (10);
}
void Naviu_1() {
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('a');
delay(100);
Keyboard.releaseAll(); aeg = 0; delay (10);
}
void Navid_1() {
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('b');
delay(100);
Keyboard.releaseAll(); aeg = 0; delay (10);
}
void GoToHomeScreen() {
Keyboard.press(KEY_LEFT_ALT);
Keyboard.write(KEY_ESC);
delay(1000);
Keyboard.releaseAll(); aeg = 0; delay (10);
}
void PrintlnLin() {
if (sync == 85) {
if (PID == 32) {
if (checksum == 255) //all buttons were released
{
if (texit == 1) {
Endcall();
}
if (enter == 1) {
Call();
}
if (next == 1) {
Next();
}
if (prev == 1) {
Prev();
}
if (navir == 1) {
Navir();
}
if (navil == 1) {
Navil();
}
if (naviu == 1) {
Naviu();
}
if (navid == 1) {
Navid();
}
if (navib1 == 1) {
Navib1();
}
if (navib2 == 1) {
Navib2();
}
aeg = 0;
texit_1 = 0;
enter_1 = 0;
navib1_1 = 0;
navib2_1 = 0;
next_1 = 0;
prev_1 = 0;
navir_1 = 0;
navil_1 = 0;
naviu_1 = 0;
navid_1 = 0;
}
//check the front exit button press
if (checksum == 0xBF) { //exit button
if (texit_1 == 0) {
texit = 1;
}
aeg = aeg + 1; //count how many times i was here without releasing
switchApp = 0;
if (aeg >= longpress) { //long press because the button was not released
texit = 0;
texit_1 = 1;// goto long press function
//Endcall();
GoToHomeScreen();
}
}
if (checksum == 223) { //front enter
if (enter_1 == 0) {
enter = 1;
}
switchApp = 0;
aeg = aeg + 1;
if (aeg >= longpress) {
enter = 0;
enter_1 = 1;
Menu();
}
}
if (message[1] == 1) {
if (navib1_1 == 0) {
navib1 = 1;
}
aeg = aeg + 1;
if (aeg >= longpress) {
navib1_1 = 1;
navib1 = 0;
Menu();
// goto long press function
}
}
if (message[1] == 8) {
if (navib2_1 == 0) {
navib2 = 1;
}
aeg = aeg + 1;
if (aeg >= longpress) {
navib2_1 = 1;
navib2 = 0;
Navib2_1();
// goto long press function
}
}
if (checksum == 239) { //next track
if (next_1 == 0) {
next = 1;
}
aeg = aeg + 1;
if (aeg >= longpress) {
next_1 = 1;
next = 0;
Next_1();
}
}
if (message[1] == 2) { //prev track
if (prev_1 == 0) {
prev = 1;
}
aeg = aeg + 1;
if (aeg >= longpress) {
prev_1 = 1;
prev = 0;
Prev_1();
}
}
if (message[0] == 4) { // navi joystick left
if (navil_1 == 0) {
navil = 1;
}
aeg = aeg + 1;
if (aeg >= longpress) {
navil_1 = 1;
navil = 0;
Navil_1();
}
}
if (message[0] == 8) { // navi joystick right
if (navir_1 == 0) {
navir = 1;
}
aeg = aeg + 1;
if (aeg >= longpress) {
navir_1 = 1;
navir = 0;
Navir_1();
}
}
//
if (message[0] == 1) { // navi joystick up
if (naviu_1 == 0) {
naviu = 1;
}
aeg = aeg + 1;
if (aeg >= longpress) {
naviu_1 = 1;
naviu = 0;
Naviu_1();
}
}
if (message[0] == 2) { // navi joystick up
if (navid_1 == 0) {
navid = 1;
}
aeg = aeg + 1;
if (aeg >= longpress) {
navid_1 = 1;
navid = 0;
Navid_1();
}
}
}
}
}
void ClearFrame() {
sync = 0;
PID = 0;
for (int i = 0; i < 4; i++) {
message[i] = 0;
}
checksum = 0;
}
system
June 3, 2016, 12:15pm
#4
Do yourself (and us) a favour - use the auto-format tool (ctrl-T) in the IDE before posting code.
Sorry, last code is edited now
Solved.Problem was that my code stuck in while loop with !Serial.available()