Switch case function

Hi there,

I have a question concerning the [ch8220]switch case[ch8221] function. The program given below is to control a small submarine via an Arduino Duemilanove µC, combined with the XBee shields for wireless communication. The first case ([ch8220]man[ch8221]) is to control the submarine while it is on the water surface. One of the commands ([ch8220]x[ch8221]) switches the case to [ch8220]aut[ch8221] (meaning [ch8220]automatic[ch8221]). In the [ch8220]aut[ch8221] case, the submarine drives a programmed figure such as an 8.

My problem arises from the fact that the submarine runs with batteries, which deliver the needed energy for the different hardware components such as engines, light & camera or pumps.
I[ch8217]d like the program to start a function which in case of a too low battery stops all hardware components and empties the diving tanks to dive up safely. When the submarine is on the water surface again, I want to switch into the [ch8220]man[ch8221] case again by sending a serial via the XBee shields ([ch8220]void check_serial[ch8221]).

As a solution, I thought of two things:

  1. An interrupt which is enabled as soon as the voltage is below a previously defined value. However, I don[ch8217]t know how I can switch from the interrupt function into the [ch8220]man[ch8221] case again.
  2. A function which reads one of the analog inputs and as soon as the value returned by [ch8220]analogRead()[ch8221] is below a defined value, the submarine dives up. I tried to do this with [ch8220]void batterie_leer[ch8221].

Additionally, I[ch8217]d like to check continuously whether a serial is send and if so, the program shall switch into the [ch8220]man[ch8221] case again.

Any help is greatly appreciated, I will reply to your answers as fast as possible.

Thanks,
BenSvuettner

This is the code…

int modus;
int ser;
int bat;
int analogin = 0;

// LICHT UND KAMERA
int light      = 12;                  // Dig - Licht an / aus per Relaisboard
int kam        = 13;                  // Dig - Kamera An / Aus

// FUNKTIONSKONSTANTEN
// DEKLARATION DER MODI IM SWITCH
const byte man = 0;
const byte aut = 1;
const byte lowbat = 2;

// LICHT UND KAMERA
const int aus = 0;
const int an = 1;

void setup()
{
  Serial.begin(9600);
  modus = man;
  bat = analogRead(analogin);

  // LICHT UND KAMERA
  pinMode(light, OUTPUT);           // 12, Dig
  pinMode(kam, OUTPUT);           // 13, Dig
}

void loop()
{
  switch(modus)
  {
  case man:
    if (Serial.available())
    {
      ser = Serial.read();
      Serial.write(ser);
      if (ser == 'K') kamera(an, 0);
      if (ser == 'k') kamera(aus, 0);
      if (ser == 'L') licht(an, 0);
      if (ser == 'l') licht(aus, 0);
      if (ser == 'x') modus = aut;
    }
    break;
  case aut:
    kamera(aus, 0);
    licht(aus, 0);
    check_serial();
    modus = aut;
    break;
  case lowbat:
    // Shut down all components and dive up
    break;
  }
}

void batterie_leer()
{
  if (bat < 511) modus = lowbat;
}

void check_serial()
{
  if (Serial.available())
  {
    ser = Serial.read();
    Serial.write(ser);
    modus = man;
  }
}

void licht(int zustand, int zeit)
{
  if (zustand == aus)
  {
    digitalWrite(light, LOW);
    delay(zeit);
  }
  if (zustand == an)
  {
    digitalWrite(light, HIGH);
    delay(zeit);
  }
}

void kamera(int zustand, int zeit)
{
  if (zustand == aus)
  {
    digitalWrite(kam, LOW);
    delay(zeit);
  }
  if (zustand == an)
  {
    digitalWrite(kam, HIGH);
    delay(zeit);
  }
}

Hi Ben,

maybe this is a good topic for the Deutsch/German Forum :-)

I read the code, but one thing I can't figure out is you case aut: part, 'cause your doing check_serial(), which tries to set the mode to manual, but the next line overrides the mode to aut immediatly. That make IMHO no sense. If I read this right, you will never go to manual mode :P

I would prefere a combination of batterie_leer() and check_serial() in the loop() just before the switch, so switch can really change the mode based on things happens.

Cheers KiWiX

serial via the XBee shields

I don't think the XBee radios will work very well underwater!

serial via the XBee shields

I don't think the XBee radios will work very well underwater!

Yes, I would imagine that, even if sealed properly, they would really have very bad range. There is a reason that microwave ovens use 2.4GHz radiation... and it involves excited water molecules...