arduino-1.6.5-r2 mac

Arduino specifieke Software
Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: arduino-1.6.5-r2 mac

Berichtdoor nicoverduin » 27 Jul 2015, 15:49

hier is alvast de code in een meer leesbare vorm:
cpp code
/*
#include <EEPROM.h>

/********************************************************
*Arduino to Homeseer 3 Plugin writen by Enigma Theatre.*
* V1.0.0.36 *
* *
*******Do not Change any values below*******************
*/

#include "Arduino.h"
#include "EEPROM.h"


//Global Variables
#define ISIP 1
const byte BoardAdd = 1;
byte Byte1, Byte2, Byte3;
int Byte4, Byte5;
char* Version = "1.0.0.36";
bool IsConnected = false;
void (*resetFunc)(void) = 0;

//******************************Ethernet Setup*****************************
#if ISIP == 1

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>


byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192, 168, 0, 40); //IP entered in HS config.
const unsigned int localPort = 8900; //port entered in HS config.
IPAddress HomeseerIP(192,168,0,10:500); //Homeseer IP address
IPAddress ServerIP(EEPROM.read(2), EEPROM.read(3), EEPROM.read(4),
EEPROM.read(5));
byte EEpromVersion = EEPROM.read(250);

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

EthernetUDP Udp;
const unsigned int ServerPort = 8888; // port to Send To

void UDPCheck() {
byte packetSize = Udp.parsePacket();
if (packetSize) {
IPAddress remote = Udp.remoteIP();
Byte1 = Udp.parseInt();
Udp.read();
Byte2 = Udp.read();
Byte3 = Udp.parseInt();
Byte4 = Udp.parseInt();
Byte5 = Udp.parseInt();
DataEvent();
}
}
#endif

//********************************Input Setup*******************************
byte InPinArray[30] = { 0 };
byte Debounce = 30;
byte count = 0;
byte NoOfInPins = 0;
int InStateArray[(sizeof(InPinArray) / sizeof(InPinArray[0]))];
unsigned long PrevDebounce[(sizeof(InPinArray) / sizeof(InPinArray[0]))];

void InputCheck() {
byte pinread;
for (count = 0; count < NoOfInPins; count++) {
if (millis() - PrevDebounce[count] > Debounce) {
pinread = (digitalRead(InPinArray[count]));
if (InStateArray[count] != pinread) {
InStateArray[count] = pinread;
PrevDebounce[count] = millis();
SendByte(BoardAdd);
SendChar(" I ");
SendByte(count + 1);
SendChar(" ");
SendByte(pinread);
Sendln();
}
}
}
}

//*******************************Analogue Setup****************************
byte AnalogPinArray[15] = { 0 };
int AnalogueDelay[(sizeof(AnalogPinArray) / sizeof(AnalogPinArray[0]))] = { 0 };
word AnalogueInvert = 0;
byte NoOfAnalogPins = 0;
int AnalogStateArray[(sizeof(AnalogPinArray) / sizeof(AnalogPinArray[0]))];
unsigned long PrevAnalogeMillis[sizeof(AnalogPinArray)
/ sizeof(AnalogPinArray[0])];

void AnalogueCheck() {
int pinread;
for (count = 0; count < NoOfAnalogPins; count++) {
if (millis() - PrevAnalogeMillis[count] > AnalogueDelay[count]) {
PrevAnalogeMillis[count] = millis();
pinread = analogRead(AnalogPinArray[count]);
if (AnalogStateArray[count] != pinread) {
AnalogStateArray[count] = pinread;
SendByte(BoardAdd);
SendChar(" A ");
SendByte(count + 1);
SendChar(" ");
if (bitRead(AnalogueInvert, count) == 1) {
SendByte(map(AnalogStateArray[count], 0, 1023, 1023, 0));
Sendln();
} else {
SendByte(AnalogStateArray[count]);
Sendln();
}
}
}
}
}

//*****************************PWM Setup****************************
byte PwmPinArray[25] = { 0 };
byte NoOfPwmPins = 0;
int PwmStateArray[(sizeof(PwmPinArray) / sizeof(PwmPinArray[0]))];
int PwmFadeTime[(sizeof(PwmPinArray) / sizeof(PwmPinArray[0]))];
int fadeTarget[(sizeof(PwmPinArray) / sizeof(PwmPinArray[0]))];
int fadeValueTweened[(sizeof(PwmPinArray) / sizeof(PwmPinArray[0]))];
int fadeValue[(sizeof(PwmPinArray) / sizeof(PwmPinArray[0]))];
unsigned long fadeTimerLast[(sizeof(PwmPinArray) / sizeof(PwmPinArray[0]))];

void PWMCheck() {
for (count = 0; count < NoOfPwmPins; count++) {
if (abs(millis() - fadeTimerLast[count]) >= 20) {
fadeTimerLast[count] = millis();
float fadeStep = (float(20) / (PwmFadeTime[count])) * 254;
if (PwmStateArray[count] > fadeValue[count]) {
fadeValue[count] = fadeValue[count] + fadeStep;
}
if (PwmStateArray[count] < fadeValue[count]) {
fadeValue[count] = fadeValue[count] - fadeStep;
}
if (fadeValue[count] == 0) {
analogWrite(PwmPinArray[count], PwmStateArray[count]);
}
fadeValue[count] = constrain(fadeValue[count], 0, 254);
fadeValueTweened[count] = Quad_easeInOut(fadeValue[count], 0, 254);
if (fadeValue[count] > 0) {
analogWrite(PwmPinArray[count], fadeValueTweened[count]);
}
}
}
}

float Quad_easeInOut(float t, float fixedScaleStart, float fixedScaleEnd) {
// float b = 0, c = 1, d = 1;
float b = fixedScaleStart;
float c = fixedScaleEnd - fixedScaleStart;
float d = fixedScaleEnd;
if ((t /= d / 2) < 1)
return c / 2 * t * t + b;
return -c / 2 * ((--t) * (t - 2) - 1) + b;
}

//*****************************Servo Setup***************************
#include <Servo.h>
byte ServoPinArray[8] = { 0 };
byte NoOfServos = 0;
Servo myservo[(sizeof(ServoPinArray) / sizeof(ServoPinArray[0]))];
int ServoPosArray[(sizeof(ServoPinArray) / sizeof(ServoPinArray[0]))];
int ServoOldPosArray[(sizeof(ServoPinArray) / sizeof(ServoPinArray[0]))];
int ServoSpeedArray[(sizeof(ServoPinArray) / sizeof(ServoPinArray[0]))];

void ServoCheck() {
for (count = 0; count < NoOfServos; count++) {
if (ServoPosArray[count] != ServoOldPosArray[count]) {
myservo[count].write(ServoPosArray[count]);
ServoOldPosArray[count] = ServoPosArray[count];
}
}
}

//***************************One Wire Setup**************************
#include <OneWire.h>
#include <DallasTemperature.h>
byte OneWirePin = EEPROM.read(1);
#define TEMPERATURE_PRECISION 9
OneWire oneWire(OneWirePin);
DallasTemperature sensors(&oneWire);
DeviceAddress tempDeviceAddress;
unsigned long PrevOneMillis = 0;
int OneUpdateTime = 5000;
float onewiretemps[15] = { 0 };

void OneWireCheck() {
if (millis() - PrevOneMillis > OneUpdateTime) {
PrevOneMillis = millis();
sensors.requestTemperatures();
for (int i = 0; i < sensors.getDeviceCount(); i++) {
if (sensors.getAddress(tempDeviceAddress, i)) {
float Temp = sensors.getTempC(tempDeviceAddress);
if (onewiretemps[i] != Temp) {
onewiretemps[i] = Temp;
SendByte(BoardAdd);
SendChar(" Rom ");
for (uint8_t i = 0; i < 8; i++) {
if (tempDeviceAddress[i] < 16)
SendChar("0");
SendByte(tempDeviceAddress[i]);
}
SendChar(" ");
SendFloat(Temp);
Sendln();
}
}
}
}
}
//******************************************************************************

//**********************************Send Data***********************************

bool UdpSend = false;

void SendConnect() {
#if ISIP == 0
Serial.print("Connect ");
Serial.println(BoardAdd);
#else
if (UdpSend == false) {
UdpSend = true;
Udp.beginPacket(ServerIP, ServerPort); //First send a connect packet to the dynamic IP stored in eeprom
Udp.print("Connect ");
Udp.print(BoardAdd);
Udp.endPacket();
if (ServerIP != HomeseerIP) {
Udp.beginPacket(HomeseerIP, ServerPort); //Then if the stored value doesn't match the pre-specified one, send a connect packet there also
Udp.print("Connect ");
Udp.print(BoardAdd);
Udp.endPacket();
}
UdpSend = false;
}
#endif
}

void SendByte(int Data) {
#if ISIP == 0
Serial.print(Data);
#else
if (UdpSend == false) {
UdpSend = true;
Udp.beginPacket(Udp.remoteIP(), ServerPort);
Udp.print(Data);
} else {
Udp.print(Data);
}
#endif
}

void SendChar(char* Data) {
#if ISIP == 0
Serial.print(Data);
#else
if (UdpSend == false) {
UdpSend = true;
Udp.beginPacket(Udp.remoteIP(), ServerPort);
Udp.print(Data);
} else {
Udp.print(Data);
}
#endif
}

void SendFloat(float Data) {
#if ISIP == 0
Serial.print(Data);
#else
if (UdpSend == false) {
UdpSend = true;
Udp.beginPacket(Udp.remoteIP(), ServerPort);
Udp.print(Data);
} else {
Udp.print(Data);
}
#endif
}

void Sendln() {
#if ISIP == 0
Serial.println();
#else
Udp.endPacket();
UdpSend = false;
#endif

}

//*****************************Data Input********************************
/*

Used Data Input Cases
D Disconnect
r reset
F PWM Fade Time Set
P PWM State Set
K Keepalive
W OneWire Pin Set
p PinMode set PWM
a PinMode AnalogInverted Set
A PinMode Analog Input Set
I PinMode Digital Input Set
O PinMode Output Set
d Input debounce time set
S Servo set Pos
s PinMode Servo set
C Connect request
c Connection established - report current status
X Board PinMode Reset
*/

void DataEvent() {

if (Byte1 == BoardAdd) {

switch (Byte2) {

case 'X':
NoOfInPins = 0;
NoOfServos = 0;
NoOfAnalogPins = 0;
NoOfPwmPins = 0;
break;

case 'c':
IsConnected = true;
#if ISIP == 1
if (Udp.remoteIP() != ServerIP) {
ServerIP = Udp.remoteIP();
EEPROM.write(2, ServerIP[0]);
EEPROM.write(3, ServerIP[1]);
EEPROM.write(4, ServerIP[2]);
EEPROM.write(5, ServerIP[3]);
}
#endif
for (count = 0; count < NoOfInPins; count++) {
int pinread;
pinread = digitalRead(InPinArray[count]);
SendByte(BoardAdd);
SendChar(" I ");
SendByte(count + 1);
SendChar(" ");
SendByte(pinread);
Sendln();
InStateArray[count] = pinread;
delay(100);
}
break;

case 'C':
SendChar("Version ");
SendByte(BoardAdd);
SendChar(" ");
SendChar(Version);
SendChar(" HS3");
Sendln();
delay(100);
SendChar("Connected ");
SendByte(BoardAdd);
Sendln();
delay(100);
IsConnected = false;
break;

case 's':
ServoPinArray[Byte3 - 1] = Byte4;
myservo[Byte3 - 1].attach(Byte4);
//ServoPosArray[count] = 90;
ServoSpeedArray[Byte3 - 1] = 0;
if (Byte3 > NoOfServos) {
NoOfServos = Byte3;
}
break;

case 'S':
ServoPosArray[Byte3 - 1] = Byte4;
break;

case 'd':
Debounce = Byte3;
break;

case 'O':
pinMode(Byte3, OUTPUT);
digitalWrite(Byte3, Byte4);
break;

case 'I':
pinMode(Byte4, INPUT);
digitalWrite(Byte4, HIGH);
if (Byte3 > NoOfInPins) {
NoOfInPins = Byte3;
}
InPinArray[Byte3 - 1] = Byte4;
break;

case 'A':
pinMode(Byte4, INPUT);
if (Byte3 > NoOfAnalogPins) {
NoOfAnalogPins = Byte3;
}
AnalogueDelay[Byte3 - 1] = Byte5;
AnalogPinArray[Byte3 - 1] = Byte4;
PrevAnalogeMillis[Byte3 - 1] = 0;
break;

case 'a':
bitWrite(AnalogueInvert, Byte3 - 1, Byte4);
break;

case 'p':
pinMode(Byte4, OUTPUT);
if (Byte3 > NoOfPwmPins) {
NoOfPwmPins = Byte3;
}
PwmPinArray[Byte3 - 1] = Byte4;
break;

case 'W':
OneWirePin = Byte3;
EEPROM.write(1, Byte3);
sensors.begin();
for (int i = 0; i < sensors.getDeviceCount(); i++) {
if (sensors.getAddress(tempDeviceAddress, i)) {
sensors.setResolution(tempDeviceAddress,
TEMPERATURE_PRECISION);
}
}
break;

case 'K':
delay(200);
SendChar("Alive ");
SendByte(BoardAdd);
Sendln();
#if ISIP == 1
if (Udp.remoteIP() != ServerIP) {
ServerIP = Udp.remoteIP();
EEPROM.write(2, ServerIP[0]);
EEPROM.write(3, ServerIP[1]);
EEPROM.write(4, ServerIP[2]);
EEPROM.write(5, ServerIP[3]);
}
#endif
break;

case 'P':
PwmStateArray[Byte3 - 1] = Byte4;
break;

case 'F':
PwmFadeTime[Byte3 - 1] = Byte4;
break;

case 'r':
SendChar("Reseting ");
Sendln();
delay(200);
resetFunc(); //call reset
break;

case 'D':
IsConnected = false;
break;

}
}
}

//*****************************Serial Event*********************************
void serialEvent() {
while (Serial.available() > 0) {
delay(17);
Byte1 = Serial.parseInt();
Serial.read();
Byte2 = Serial.read();
Byte3 = Serial.parseInt();
Byte4 = Serial.parseInt();
Byte5 = Serial.parseInt();
DataEvent();
}
}

//*****************************Setup Void*********************************
void setup() {
for (count = 0; count < NoOfPwmPins; count++) {
PwmFadeTime[count] = 0;
PwmStateArray[count] = 0;
fadeValue[count] = 0;
}

#if ISIP == 1
if (EEpromVersion != 22) {
ServerIP = HomeseerIP;
EEPROM.write(2, ServerIP[0]);
EEPROM.write(3, ServerIP[1]);
EEPROM.write(4, ServerIP[2]);
EEPROM.write(5, ServerIP[3]);
EEPROM.write(250, 22); //Store the version where the eeprom data layout was last changed
EEpromVersion = 22;
}
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Udp.setTimeout(0);
#else
Serial.begin(115200);
Serial.flush();
Serial.setTimeout(0);
#endif
delay(1000);
IsConnected = false;
SendConnect();
}

//*****************************Loop Void*********************************
void loop() {
#if ISIP == 1
UDPCheck();
#endif
if (IsConnected == true) {
InputCheck();
AnalogueCheck();
PWMCheck();
ServoCheck();
if (OneWirePin > 0) {
OneWireCheck();
}
}
}

//****************************************End**************************************************
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Advertisement

Berichten: 12
Geregistreerd: 07 Jan 2015, 20:46

Re: arduino-1.6.5-r2 mac

Berichtdoor walberg » 27 Jul 2015, 15:53

Oh ondertussen is er net een reactie geplaatst. Ik heb ondertussen ook nog eens de poort eraf gelaten, en ik heb ook de poort geprobeerd in te voeren na een komma in plaats van een dubbele punt, mocht niet baten dus uiteindelijk gewoon alleen het ip adres maar ook daar komen foutmeldingen op, ongeacht de versie en ongeacht op de MAC of PC. Enige verschil dat ik opmerk is dat ik bij de pc's ook nog even geduld moet hebben omdat ze de poort nog moeten aanmaken. Ik krijg pas later een poort ter beschikking in het hulpmiddelen menu. Bij de Mac staat de beschikbare poort direct klaar. Bij de eerste keer dat ik op het netboekje Arduino installeerde kwamen er ook extra library bestanden mee die in /programfiles werden geplaatst. Zowel met deze als zonder deze geprobeerd.

Berichten: 12
Geregistreerd: 07 Jan 2015, 20:46

Re: arduino-1.6.5-r2 mac

Berichtdoor walberg » 27 Jul 2015, 15:59

Ik ga het nog eens proberen met de aangepaste code van mijnheer Verduin. Ik zal Textwrangler gebruiken om er zeker van te zijn dat er geen extra opmaakcodes meegaan.

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: arduino-1.6.5-r2 mac

Berichtdoor nicoverduin » 27 Jul 2015, 16:11

Dit is gewoon jouw source hoor. Alleen is het geformatteerd beter te lezen. Ook voor anderen.

zie: viewtopic.php?f=16&t=903
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Vorige

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 91 gasten