MoritzDornseifer
Published © GPL3+

IoT Busy Board

A blinking and beeping busy board with IoT cloud capabilities.

BeginnerShowcase (no instructions)747
IoT Busy Board

Things used in this project

Hardware components

Arduino MKR WiFi 1010
Arduino MKR WiFi 1010
×1
Arduino MKR Proto Shield
Arduino MKR Proto Shield
×1
LiPo 3.7 V 350 mAh
×1

Software apps and online services

Arduino Web Editor
Arduino Web Editor
Arduino IoT Cloud
Arduino IoT Cloud

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

circuit diagram

Code

code for IoT busy board

Arduino
/* 
  Sketch generated by the Arduino IoT Cloud Thing "..."
  https://create.arduino.cc/cloud/things/... 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudSwitch randomMode;
  CloudSwitch volumeOn;

  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"

//intputs

const int Poti1Pin = A0;
const int Poti2Pin = A1;
const int Poti3Pin = A2;
const int Poti4Pin = A3;
const int Poti5Pin = A4;

const int JoystickXPin = A5;
const int JoystickYPin = A6;

const int Switch1Pin = 8;
const int Switch2Pin = 9;
const int Switch3Pin = 10;
//const int Switch4Pin = 11; => this is why I got the ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to mqtts-sa.iot.arduino.cc:8883 Error (maybe also in combination with tone()?)
const int Switch4Pin = 5;

//outputs

const int GaugePin = 0;

const int Led1Pin = 1;
const int Led2Pin = 2;
const int Led3Pin = 3;
//const int Led4Pin = 4; => interferred by tone()
//const int Led4Pin = 12; => no connection to IoT cloud
const int Led4Pin = 7;


const int Buzzer1Pin = 6;
//const int Buzzer2Pin = 7; => pwm pin used for led 4. changed to 13
const int Buzzer2Pin = 13;

//

bool LedStatus = false;

bool BlinkOnLed1 = false;
unsigned long MillisWhenBlink1Started;

bool BlinkOnLed3 = false;
unsigned long MillisWhenBlink3Started;

const int GaugePinMaxValue = 25;
bool GaugePinValueRising = true;

unsigned int Buzzer1Frequency;
unsigned long Buzzer1ToneDuration;
unsigned long MillisWhenBuzzer1Started = 0;
bool Buzzer1On;

unsigned int Buzzer2Frequency;
unsigned long Buzzer2ToneDuration;
unsigned long MillisWhenBuzzer2Started = 0;
bool Buzzer2On;

int BuzzerTheshold = 10;

int JoyStickXNeutralPosition = 527;
int JoyStickXAnalogValue;

int JoyStickYNeutralPosition = 535;
int JoyStickYAnalogValue;

int AnalogInputs[7]; //stores all analog inputs
int AnalogInputsBackup[7];

bool SwitchValues[4];
bool SwitchValuesBackup[4];

int AnalogIndex[7] = {0, 1, 2, 3, 4, 5, 6};
int SwitchIndex[4] = {0, 1, 2, 3};
int RandomNumber;


void beep(int BuzzerPin, unsigned long MillisWhenBuzzerStarted, unsigned long BuzzerToneDuration, unsigned int BuzzerFrequency)
{
  //buzzer beeps half of the duration and pauses for the other half
  if(millis() <= MillisWhenBuzzerStarted + BuzzerToneDuration/2)
  {
    if(volumeOn)
    {
      tone(BuzzerPin, BuzzerFrequency);
    }
    else
    {
      noTone(BuzzerPin);
    }
  }
  else //other half of duration, where buzzer is quiet (pauses between tones)
  {
    noTone(BuzzerPin);
  }
}

void blink(int LedPin, unsigned long MillisWhenBlinkStarted, unsigned long BlinkDuration)
{
  //LED is on for half of the duration and pauses for the other half
  if(millis() <= MillisWhenBlinkStarted + BlinkDuration/2)
  {
    analogWrite(LedPin, 255);
  }
  else //other half of duration, where led is off
  {
    analogWrite(LedPin, 0);
  }
}


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  
  //inputs
  pinMode(Poti1Pin, INPUT);
  pinMode(Poti2Pin, INPUT);
  pinMode(Poti3Pin, INPUT);
  pinMode(Poti4Pin, INPUT);
  pinMode(Poti5Pin, INPUT);
  
  pinMode(JoystickXPin, INPUT);
  pinMode(JoystickYPin, INPUT);
  
  pinMode(Switch1Pin, INPUT);
  pinMode(Switch2Pin, INPUT);
  pinMode(Switch3Pin, INPUT);
  pinMode(Switch4Pin, INPUT);
  
  //outputs

  pinMode(GaugePin, OUTPUT);
  
  pinMode(Buzzer1Pin, OUTPUT);
  pinMode(Buzzer2Pin, OUTPUT);
  
  pinMode(Led1Pin, OUTPUT);
  pinMode(Led2Pin, OUTPUT);
  pinMode(Led3Pin, OUTPUT);
  pinMode(Led4Pin, OUTPUT);

  // Defined in thingProperties.h
  initProperties();

  // 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
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  delay(5000); 
  
  Serial.println("Setup complete");
}

void loop() {
  ArduinoCloud.update();
  // Your code here
  
  AnalogInputsBackup[0] = map(analogRead(Poti1Pin), 0, 1023, 0, 500);
  AnalogInputsBackup[1] = map(analogRead(Poti2Pin), 0, 1023, 0, 500);
  AnalogInputsBackup[2] = map(analogRead(Poti3Pin), 0, 1023, 0, 500);
  AnalogInputsBackup[3] = map(analogRead(Poti4Pin), 0, 1023, 0, 500);
  AnalogInputsBackup[4] = map(analogRead(Poti5Pin), 0, 1023, 0, 500);
  
  SwitchValuesBackup[0] = digitalRead(Switch1Pin);
  SwitchValuesBackup[1] = digitalRead(Switch2Pin);
  SwitchValuesBackup[2] = digitalRead(Switch3Pin);
  SwitchValuesBackup[3] = digitalRead(Switch4Pin);
  
  
  JoyStickXAnalogValue = analogRead(JoystickXPin);
  JoyStickYAnalogValue = analogRead(JoystickYPin);
  
  //both joystick values shall be 0 in middle position.
  
  if(JoyStickXAnalogValue < JoyStickXNeutralPosition + 8 * BuzzerTheshold && JoyStickXAnalogValue > JoyStickXNeutralPosition - 8 * BuzzerTheshold &&
    JoyStickYAnalogValue < JoyStickYNeutralPosition + 8 * BuzzerTheshold && JoyStickYAnalogValue > JoyStickYNeutralPosition - 8 * BuzzerTheshold)
  {
    //joystick is in neutral position (+-) ==> input shall be 0
    AnalogInputsBackup[5] = 0;
    AnalogInputsBackup[6] = 0;
  }
  else
  {
    AnalogInputsBackup[5] = map(JoyStickXAnalogValue, 0, 1023, 0, 500);
    AnalogInputsBackup[6] = map(JoyStickYAnalogValue, 1023, 0, BuzzerTheshold+1, 500);
  }
  
  //indices gets changed for random mode
  AnalogInputs[0] = AnalogInputsBackup[AnalogIndex[0]];
  AnalogInputs[1] = AnalogInputsBackup[AnalogIndex[1]];
  AnalogInputs[2] = AnalogInputsBackup[AnalogIndex[2]];
  AnalogInputs[3] = AnalogInputsBackup[AnalogIndex[3]];
  AnalogInputs[4] = AnalogInputsBackup[AnalogIndex[4]];
  AnalogInputs[5] = AnalogInputsBackup[AnalogIndex[5]];
  AnalogInputs[6] = AnalogInputsBackup[AnalogIndex[6]];
  
  SwitchValues[0] = SwitchValuesBackup[SwitchIndex[0]];
  SwitchValues[1] = SwitchValuesBackup[SwitchIndex[1]];
  SwitchValues[2] = SwitchValuesBackup[SwitchIndex[2]];
  SwitchValues[3] = SwitchValuesBackup[SwitchIndex[3]];

  //frequencies see https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody
  
  //buzzer 1
  
  Buzzer1Frequency = map(AnalogInputs[5], 0, 500, 31, 4978);
  Buzzer1ToneDuration = AnalogInputs[6];
  
  if(Buzzer1On == false && Buzzer1ToneDuration > BuzzerTheshold)
  {
    Buzzer1On = true;
    MillisWhenBuzzer1Started = millis();
  }
  
  if(Buzzer1On == true) //see below, only works simultaneously
  {
    if(millis() > MillisWhenBuzzer1Started + Buzzer1ToneDuration)
    {
      Buzzer1On = false;
    }
  } 
  else
  {
    Buzzer1On = false;
  }
  
  if(Buzzer1On == true)
  {
    beep(Buzzer1Pin, MillisWhenBuzzer1Started, Buzzer1ToneDuration, Buzzer1Frequency);
  }
  else
  {
    noTone(Buzzer1Pin);
  }
  

  //buzzer 2
  
  Buzzer2Frequency = map(AnalogInputs[1], 0, 500, 31, 4978);
  Buzzer2ToneDuration = AnalogInputs[3];
  
  if(Buzzer2On == false && Buzzer2ToneDuration > BuzzerTheshold)
  {
    Buzzer2On = true;
    MillisWhenBuzzer2Started = millis();
  }
  
  if(Buzzer2On == true)
  {
    if(millis() > MillisWhenBuzzer2Started + Buzzer2ToneDuration)
    {
      Buzzer2On = false;
    }
    else
    {
      beep(Buzzer2Pin, MillisWhenBuzzer2Started, Buzzer2ToneDuration, Buzzer2Frequency);
    }
  }
  else
  {
    noTone(Buzzer2Pin);
  }
  
  //both buzzers work at the same time. however, buzzer 1 only works if buzzer 2 beeps as well. buzzer 2 works individually.
  //if buzzer 2 is "off", use it for buzzer 1's inputs to work individually as well
  if(Buzzer2On == false && Buzzer1On == true)
  {
    noTone(Buzzer1Pin);
    beep(Buzzer2Pin, MillisWhenBuzzer1Started, Buzzer1ToneDuration, Buzzer1Frequency);
  }

  
  
  //gauge
  analogWrite(GaugePin, map(AnalogInputs[4], 0, 500, 0, GaugePinMaxValue));
  
  //led 1
  if(SwitchValues[0] == true)
  {
    //blink in different intervals
    
    if(BlinkOnLed1 == false && AnalogInputs[0] > BuzzerTheshold)
    {
      BlinkOnLed1 = true;
      MillisWhenBlink1Started = millis();
    }
    
    if(BlinkOnLed1 == true)
    {
      if(millis() > MillisWhenBlink1Started + AnalogInputs[0])
      {
        BlinkOnLed1 = false;
      }
    }
    
    if(BlinkOnLed1 == true)
    {
      blink(Led1Pin, MillisWhenBlink1Started, AnalogInputs[0]);
    }
    else
    {
      analogWrite(Led1Pin, 255);
    }
  }
  else
  {
    //dim
    analogWrite(Led1Pin, map(AnalogInputs[0], 0, 500, 0, 255));
  }
  
  //led 3
  
  if(SwitchValues[2] == true)
  {
    //blink in different intervals
    
    if(BlinkOnLed3 == false && AnalogInputs[2] > BuzzerTheshold)
    {
      BlinkOnLed3 = true;
      MillisWhenBlink3Started = millis();
    }
    
    if(BlinkOnLed3 == true)
    {
      if(millis() > MillisWhenBlink3Started + AnalogInputs[2])
      {
        BlinkOnLed3 = false;
      }
    }
    
    if(BlinkOnLed3 == true)
    {
      blink(Led3Pin, MillisWhenBlink3Started, AnalogInputs[2]);
    }
    else
    {
      analogWrite(Led3Pin, 255);
    }

    
  }
  else
  {
    //dim
    analogWrite(Led3Pin, map(AnalogInputs[2], 0, 500, 0, 255));
  }
  
  //led 2
  if(SwitchValues[1] == false)
  {
    analogWrite(Led2Pin, 10);
  }
  else
  {
    analogWrite(Led2Pin, 0);
  }
  
  //led 4
  if(SwitchValues[3] == false)
  {
    analogWrite(Led4Pin, 255);
  }
  else
  {
    analogWrite(Led4Pin, 0);
  }
}


void onRandomModeChange() {
  // Do something
  
  if(randomMode == true)
  {
    RandomNumber = int(random(0,7));
    
    for(int i=0; i <= 6; i++)
    {
      if(RandomNumber + i <= 6)
      {
        AnalogIndex[i] = RandomNumber + i;
      }
      else // RandomNumber + i > 6 ==> from 7 onwards
      {
        AnalogIndex[i] = RandomNumber + i - 7;
      }
    }
    
    for(int k=0; k <= 3; k++)
    {
      if(RandomNumber + k <= 3)
      {
        SwitchIndex[k] = RandomNumber + k;
      }
      else // RandomNumber + k > 3 ==> from 4 onwards
      {
        SwitchIndex[k] = RandomNumber + k - 4;
      }
    }
    
    
  }
  else
  {
    AnalogIndex[0] = 0;
    AnalogIndex[1] = 1;
    AnalogIndex[2] = 2;
    AnalogIndex[3] = 3;
    AnalogIndex[4] = 4;
    AnalogIndex[5] = 5;
    AnalogIndex[6] = 6;
    
    SwitchIndex[0] = 0;
    SwitchIndex[1] = 1;
    SwitchIndex[2] = 2;
    SwitchIndex[3] = 3;
  }
  
}


void onVolumeOnChange() {
  // Do something
}

Credits

MoritzDornseifer
0 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.