This should be all relevant code:
#include <TLx493D_inc.hpp>
#include <Servo.h>
#include <Wire.h>
using namespace ifx::tlx493d;
TLx493D_W2BW dut(Wire, TLx493D_IIC_ADDR_A0_e);
/*
Sketch generated by the Arduino IoT Cloud Thing "Snootybug"
https://create.arduino.cc/cloud/things/1bc1e18f-324a-496a-a5e5-8881a02a7677
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float magn_X;
float magn_Y;
float magn_Z;
float x_speed;
float y_speed;
int lB_slide;
int lF_slide;
int rB_slide;
int rF_slide;
int slider_speed;
int slider_time;
bool press_E;
bool press_N;
bool press_NE;
bool press_NW;
bool press_S;
bool press_SE;
bool press_stop;
bool press_SW;
bool press_treat;
bool press_W;
bool switch_continuous;
bool switch_phoneremote;
bool switch_timed;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <WiFiNINA.h>
#include <utility/wifi_drv.h>
#include <math.h>
#include "motorclass.h"
#define RED 0x00ff00
#define AMBER 0x80ff00
#define YELLOW 0xffa000
#define GREEN 0xff0000
#define CYAN 0xff00ff
#define BLUE 0x0000ff
#define MAGENTA 0x00ffff
#define WHITE 0xffddff
#define BLACK 0x000000
void normalise_speed(void);
void set_colour(int c);
void set_colour(int r, int g, int b);
const uint8_t POWER_PIN = 8;
Servo dispenser;
float mx, my, mz;
enum modes
{
timed,
continuous,
phoneremote,
limbo
};
modes mode;
motorclass motor_LB(0, 1, 1);
motorclass motor_RB(2, 3, 1);
motorclass motor_RF(4, 5, 1);
motorclass motor_LF(6, 7, -1);
void setup() {
// Defined in thingProperties.h
initProperties();
WiFiDrv::pinMode(25, OUTPUT);
WiFiDrv::pinMode(26, OUTPUT);
WiFiDrv::pinMode(27, OUTPUT);
WiFiDrv::digitalWrite(25, 0);
WiFiDrv::digitalWrite(26, 0);
WiFiDrv::digitalWrite(27, 0);
set_colour(RED);
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
set_colour(AMBER);
// My setups
switch_continuous = false;
switch_phoneremote = true;
switch_timed = false;
mode = phoneremote;
x_speed = 0.;
y_speed = 0.;
dispenser.attach(20);
Wire.begin();
dut.begin();
dut.setSensitivity(TLx493D_FULL_RANGE_e);
set_colour(YELLOW);
}
void loop() {
// Your code here
float length, alfa;
int channel_r;
int channel_g;
int channel_b;
dispenser.write(15);
do
{
set_colour(GREEN);
while(1)
{
double t, x, y, z;
set_colour(CYAN);
if (dut.getMagneticFieldAndTemperature(&x, &y, &z, &t))
{
magn_X = x;
magn_Y = y;
magn_Z = z;
set_colour(GREEN);
}
else
{
set_colour(RED);
}
ArduinoCloud.update();
delay(100);
}
}
/*
Since PressN is READ_WRITE variable, onPressNChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressNChange() {
// Add your code here to act upon PressN change
if (!press_N) return;
y_speed += 5.;
normalise_speed();
}
/*
Since PressNE is READ_WRITE variable, onPressNEChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressNEChange() {
// Add your code here to act upon PressNE change
if (!press_NE) return;
y_speed += 3.53553390593;
x_speed += 3.53553390593;
normalise_speed();
}
/*
Since PressE is READ_WRITE variable, onPressEChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressEChange() {
// Add your code here to act upon PressE change
if (!press_E) return;
x_speed += 5.;
normalise_speed();
}
/*
Since PressSE is READ_WRITE variable, onPressSEChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressSEChange() {
// Add your code here to act upon PressSE change
if (!press_SE) return;
y_speed -= 3.53553390593;
x_speed += 3.53553390593;
normalise_speed();
}
/*
Since PressS is READ_WRITE variable, onPressSChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressSChange() {
// Add your code here to act upon PressS change
if (!press_S) return;
y_speed -= 5.;
normalise_speed();
}
/*
Since PressSW is READ_WRITE variable, onPressSWChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressSWChange() {
// Add your code here to act upon PressSW change
if (!press_SW) return;
y_speed -= 3.53553390593;
x_speed -= 3.53553390593;
normalise_speed();
}
/*
Since PressW is READ_WRITE variable, onPressWChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressWChange() {
// Add your code here to act upon PressW change
if (!press_W) return;
x_speed -= 5;
normalise_speed();
}
/*
Since PressNW is READ_WRITE variable, onPressNWChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressNWChange() {
// Add your code here to act upon PressNW change
if (!press_NW) return;
y_speed += 3.53553390593;
x_speed -= 3.53553390593;
normalise_speed();
}
/*
Since PressStop is READ_WRITE variable, onPressStopChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressStopChange() {
// Add your code here to act upon PressStop change
y_speed = 0.;
x_speed = 0.;
if (press_stop)
{
motor_LF.set_speed(-150); //
motor_RF.set_speed(150);
motor_LB.set_speed(150);
motor_RB.set_speed(-150);
delay(1500);
motor_LF.set_speed(0); //
motor_RF.set_speed(0);
motor_LB.set_speed(0);
motor_RB.set_speed(0);
delay(1500);
}
}
/*
Since PressTreat is READ_WRITE variable, onPressTreatChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPressTreatChange() {
// Add your code here to act upon PressTreat change
if (press_treat)
{
dispenser.write(70);
delay(1000);
dispenser.write(15);
}
}
/*
Since SwitchTime is READ_WRITE variable, onSwitchTimeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSwitchTimeChange() {
// Add your code here to act upon SwitchTime change
}
/*
Since SwitchContinuous is READ_WRITE variable, onSwitchContinuousChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSwitchContinuousChange() {
// Add your code here to act upon SwitchContinuous change
if (switch_continuous)
{
mode = continuous;
switch_timed = false;
switch_phoneremote = false;
WiFiDrv::digitalWrite(25, 1);
WiFiDrv::digitalWrite(26, 0);
WiFiDrv::digitalWrite(27, 0);
}
else
{
if (mode == continuous)
{
mode = limbo;
WiFiDrv::digitalWrite(25, 0);
}
}
}
/*
Since SwitchTimed is READ_WRITE variable, onSwitchTimedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSwitchTimedChange() {
// Add your code here to act upon SwitchTimed change
if (switch_timed)
{
mode = timed;
switch_continuous = false;
switch_phoneremote = false;
WiFiDrv::digitalWrite(25, 0);
WiFiDrv::digitalWrite(26, 1);
WiFiDrv::digitalWrite(27, 0);
}
else
{
if (mode == timed)
{
mode = limbo;
WiFiDrv::digitalWrite(26, 0);
}
}
}
/*
Since SwitchPhoneremote is READ_WRITE variable, onSwitchPhoneremoteChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSwitchPhoneremoteChange() {
// Add your code here to act upon SwitchPhoneremote change
if (switch_phoneremote)
{
mode = phoneremote;
switch_continuous = false;
switch_timed = false;
WiFiDrv::digitalWrite(25, 0);
WiFiDrv::digitalWrite(26, 0);
WiFiDrv::digitalWrite(27, 1);
}
else
{
if (mode == phoneremote)
{
mode = limbo;
WiFiDrv::digitalWrite(27, 0);
}
}
}
/*
Since SliderTime is READ_WRITE variable, onSliderTimeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSliderTimeChange() {
// Add your code here to act upon SliderTime change
}
/*
Since SliderSpeed is READ_WRITE variable, onSliderSpeedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSliderSpeedChange() {
// Add your code here to act upon SliderSpeed change
}
/*
Since YSpeed is READ_WRITE variable, onYSpeedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onYSpeedChange() {
// Add your code here to act upon YSpeed change
}
/*
Since XSpeed is READ_WRITE variable, onXSpeedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onXSpeedChange() {
// Add your code here to act upon XSpeed change
}
void normalise_speed(void)
{
float speed, ratio;
speed = x_speed * x_speed + y_speed * y_speed;
if (speed <= 2500)
return;
ratio = 2500. / speed;
x_speed *= ratio;
y_speed *= ratio;
}
/*
Since PhoneAx is READ_WRITE variable, onPhoneAxChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPhoneAxChange() {
// Add your code here to act upon PhoneAx change
}
/*
Since PhoneAy is READ_WRITE variable, onPhoneAyChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPhoneAyChange() {
// Add your code here to act upon PhoneAy change
}
/*
Since PhoneAz is READ_WRITE variable, onPhoneAzChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPhoneAzChange() {
// Add your code here to act upon PhoneAz change
}
/*
Since GyroZ is READ_WRITE variable, onGyroZChange() is
executed every time a new value is received from IoT Cloud.
*/
void onGyroZChange() {
// Add your code here to act upon GyroZ change
}
void set_colour(int c)
{
set_colour(c >> 16, (c >> 8) & 255, c & 255);
}
void set_colour(int r, int g, int b)
{
WiFiDrv::analogWrite(25, r);
WiFiDrv::analogWrite(26, g);
WiFiDrv::analogWrite(27, b);
}
/*
Since LFSlide is READ_WRITE variable, onLFSlideChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLFSlideChange() {
// Add your code here to act upon LFSlide change
}
/*
Since RFSlide is READ_WRITE variable, onRFSlideChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRFSlideChange() {
// Add your code here to act upon RFSlide change
}
/*
Since LBSlide is READ_WRITE variable, onLBSlideChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLBSlideChange() {
// Add your code here to act upon LBSlide change
}
/*
Since RBSlide is READ_WRITE variable, onRBSlideChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRBSlideChange() {
// Add your code here to act upon RBSlide change
}
/*
Since MagnX is READ_WRITE variable, onMagnXChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMagnXChange() {
// Add your code here to act upon MagnX change
mx = magn_X;
}
/*
Since MagnY is READ_WRITE variable, onMagnYChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMagnYChange() {
// Add your code here to act upon MagnY change
my = magn_Y;
}
/*
Since MagnZ is READ_WRITE variable, onMagnZChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMagnZChange() {
// Add your code here to act upon MagnZ change
mz = magn_Z;
}