I'm not sure if this is a (specific) Uno problem or a project related problem.
My sketch will only work right after uploading. Resetting the arduino while still being powered by the pc(mac) will show a working sketch. Only when i disconnect and use a external powersupply or the same usb powersupply, the sketch won't start.
For debugging i've included a Serial.println() in setup() and connected the arduino's TX to a serial port (via TTL to serial). This also won't show the Serial.prinln() when the arduino is powercycled after uploading the sketch.
#include "velbus.h"
#include <SPI.h>
#include <stdio.h>
#define INT8U unsigned char
#define BufferLength 128
int errorled = 7;
int scanbutton = 6;
unsigned long buttonDebounce = millis() + 75;
int clearfilter = 0;
int wtwled_1 = 3;
int wtwled_2 = 4;
int wtwled_3 = 5;
boolean wtwsend = false;
byte wtwsend_address = 0x00;
byte wtwsend_clear = 0x00;
VelbusPacket vbpbuffer[BufferLength];
VelbusPacket receivedpacket;
VelbusPacket sendpacket;
int BufferStart = 0;
int BufferEnd = 0;
void setup()
{
pinMode(scanbutton, INPUT);
pinMode(errorled,OUTPUT);
pinMode(wtwled_1, OUTPUT);
pinMode(wtwled_2, OUTPUT);
pinMode(wtwled_3, OUTPUT);
digitalWrite(errorled, HIGH);
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
delay(2000);
VELBUS.begin(); // init velbus : default baudrate = 16k666, default SPI chip select pin = 10
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt when new packet has been received
Serial.begin(9600);
Serial.println("debug: Arduino start");
}
void MCP2515_ISR()
{
while (VELBUS.checkReceive() == CAN_MSGAVAIL)
{
if (VELBUS.receivepacket(receivedpacket))
{
int BufferEndNow = BufferEnd;
BufferEndNow++; // fill receivebuffer with new packet so we can process it in the loop
if (BufferEndNow >= BufferLength) BufferEndNow = 0;
if (BufferEndNow != BufferStart)
{
vbpbuffer[BufferEndNow] = receivedpacket;
BufferEnd = BufferEndNow;
}
}
}
}
void loop()
{
if(BufferStart != BufferEnd) // check if buffer is not empty
{
BufferStart ++;
if (BufferStart >= BufferLength) BufferStart = 0;
if(vbpbuffer[BufferStart].Address == 0xA3)
{
if(vbpbuffer[BufferStart].dataLen == 4)
{
if((vbpbuffer[BufferStart].Data[0] == 0x00) && (vbpbuffer[BufferStart].Data[1] == 0x01) && (vbpbuffer[BufferStart].Data[2] == 0x00))
{
Serial.println("debug: WTW Stand 1");
digitalWrite(wtwled_1, HIGH);
digitalWrite(wtwled_2, LOW);
digitalWrite(wtwled_3, LOW);
wtwsend = true;
wtwsend_address = 0x01;
}
else if ((vbpbuffer[BufferStart].Data[0] == 0x00) && (vbpbuffer[BufferStart].Data[1] == 0x02) && (vbpbuffer[BufferStart].Data[2] == 0x00))
{
Serial.println("debug: WTW Stand 2");
digitalWrite(wtwled_1, LOW);
digitalWrite(wtwled_2, HIGH);
digitalWrite(wtwled_3, LOW);
wtwsend = true;
wtwsend_address = 0x02;
}
else if ((vbpbuffer[BufferStart].Data[0] == 0x00) && (vbpbuffer[BufferStart].Data[1] == 0x08) && (vbpbuffer[BufferStart].Data[2] == 0x00))
{
Serial.println("debug: WTW Stand 3");
digitalWrite(wtwled_1, LOW);
digitalWrite(wtwled_2, LOW);
digitalWrite(wtwled_3, HIGH);
wtwsend = true;
wtwsend_address = 0x08;
}
if (wtwsend == true)
{
sendpacket.RTR = false;
sendpacket.Priority = Priority::low;
sendpacket.Address = 0xA3;
sendpacket.Data[0] = 0xF6;
sendpacket.Data[1] = wtwsend_address;
sendpacket.dataLen = 2;
if (VELBUS.sendpacket(sendpacket)) digitalWrite(errorled,HIGH); else digitalWrite(errorled, LOW);
delay(90); // delay 90ms
wtwsend_clear = 0x0B - wtwsend_address; // ClearLed 0x0B (led 1,2,8 ) minus new setled
sendpacket.RTR = false;
sendpacket.Priority = Priority::low;
sendpacket.Address = 0xA3;
sendpacket.Data[0] = 0xF5;
sendpacket.Data[1] = wtwsend_clear;
sendpacket.dataLen = 2;
if (VELBUS.sendpacket(sendpacket)) digitalWrite(errorled,HIGH); else digitalWrite(errorled, LOW);
wtwsend = false;
}
}
// feedback for homeseer
Serial.print("canbus: ");
Serial.print(vbpbuffer[BufferStart].Address,16);
Serial.print(" ");
for(int i = 0; i<vbpbuffer[BufferStart].dataLen; i++) // print the data
{
Serial.print(vbpbuffer[BufferStart].Data[i], 16);
Serial.print(" ");
}
Serial.println("");
}
}
// EXAMPLE OF SENDING VELBUS PACKET
// below we check the button on pin 6. When the pin is LOW for 75ms (debounce time of 75ms) we send a scan packet to the bus
if (digitalRead(scanbutton) == HIGH)
{
if (millis() > buttonDebounce && buttonDebounce > 0)
{
if (clearfilter == 1)
{
sendpacket.RTR = false;
sendpacket.Priority = Priority::low;
sendpacket.Address = 0xA3;
sendpacket.Data[0] = 0xF5;
sendpacket.Data[1] = 0x04;
sendpacket.dataLen = 2;
if (VELBUS.sendpacket(sendpacket)) digitalWrite(errorled,HIGH); else digitalWrite(errorled, LOW); // if you send a next packet within 90ms after the previous packet, VELBUS.sendpacket() will return false and the packet will not be sent (to avoid flooding the velbus). To avoid this, you can check VELBUS.cansend() before sending the packet
buttonDebounce = 0;
clearfilter = 0;
}
else
{
sendpacket.RTR = false;
sendpacket.Priority = Priority::low;
sendpacket.Address = 0xA3;
sendpacket.Data[0] = 0xF7;
sendpacket.Data[1] = 0x04;
sendpacket.dataLen = 2;
if (VELBUS.sendpacket(sendpacket)) digitalWrite(errorled,HIGH); else digitalWrite(errorled, LOW); // if you send a next packet within 90ms after the previous packet, VELBUS.sendpacket() will return false and the packet will not be sent (to avoid flooding the velbus). To avoid this, you can check VELBUS.cansend() before sending the packet
buttonDebounce = 0;
clearfilter = 1;
}
}
}
else
{
buttonDebounce = millis() + 75;
}
}