arduino hovercraft

Hi there,

I am a Dutch engineering student, and i need some help with Arduino. We need to build a hovercraft that can detect objects, and react differently every time. To achieve that in need to program my arduino, but there's a catch. Although i know what i need in the code, i do not know how to write it in c++.

what’s supposed to happen is this: I fly the hovercraft remotely controlled towards a buoy/sea mark. I will gently nudge the buoy, and that will activate a button/pressure sensor. This is the beginning! The push of button will go to the arduino. The signal used to be LOW on the sensor port, but during the contact it is HIGH. That should trigger the arduino to send a current to transistor 1. This transistor should than be triggered for 3 seconds. An external circuit will take care of the special effect.

When I hit the next buoy, the code should repeat it self. Except for the fact that now transistor two should be activated for 3 seconds.

I thought that i should do that by using a mathematical row. Activate N+1. N is the previous active transistor. so when i hit the first buoy, it is 0+1=1. This should be capable to activate 10 different effects. But i can't find a proper way to write these code descriptions in C++.
Would you please help me? A basic set up in code should be enough i guess.

Alex

Activate N+1. N is the previous active transistor. so when i hit the first buoy, it is 0+1=1.

All that is needed is to store N in a variable, and increment it when a buoy is hit. (N++;) Trivially simple, compared to detecting that a buoy has been hit, and doing the rest of the stuff needed.

Seems pretty straightforward. Every time the button is pressed, you add 1 to a counter.
Have two arrays, one for the pin that drives the transistor, and one that says how long the transistor is driven.

pseudocode:

if ( digitalRead (sensor_button) == HIGH ){
sensor_count = sensor_count +1;
digitalWrite ( pinMap [sensor_count] );
delay (onTime [sensor_count] );
}

So it should be something like:

int sensor_button = 2;         
int transistor1 = 1;
int transistor2 = 11;
int transistor3 = 3;
int transistor4 = 4;
int transistor5 = 5;
int transistor6 = 6;
int transistor7 = 7;
int transistor8 = 8;
int transistor9 = 9;
int transistor10 = 10;

void setup() {
  pinMode(sensor_button, INPUT);      
  pinMode(transistor1, OUTPUT);   
  pinMode(transistor2, OUTPUT);  
 pinMode(transistor3, OUTPUT);  
 pinMode(transistor4, OUTPUT);  
 pinMode(transistor5, OUTPUT);  
 pinMode(transistor6, OUTPUT);  
 pinMode(transistor7, OUTPUT);  
 pinMode(transistor8, OUTPUT);  
 pinMode(transistor9, OUTPUT);  
 pinMode(transistor10, OUTPUT);   
}

if ( digitalRead (sensor_button) == HIGH ){
sensor_count = sensor_count +1;
digitalWrite ( pinMap [sensor_count] );
delay (onTime [sensor_count] );
}
}

  }
}

what do i need to change to delay the on/off for 3 seconds

you also need to define the two arrays:

byte pinMap [ 1,11, 3,4,5,6,7,8,9,10]; // note: 0,1 are used for serial comm's
byte onTime [3,6,9,12,15,18,21,24,27,30]; // transistor on times

and the variable holding the array index:
byte sensor_count = 0;

and don't forget void loop() as AWOL indicated.

Actually, these are delays in mS,

byte onTime [3,6,9,12,15,18,21,24,27,30]; // transistor on times

so you probably want to make that an int datatype so you can go bigger, up to 65535mS.

I personally would not use delay() there, as you uC is not able to read the sensor again while delaying.
I would program it blink-without-delay style, where you capture the time the sensor was triggered, set a flag to note that time is elapsing, and check to see if enough time had elapsed to stop the action.
That's more than I could write up as an example tho. Delay() will get you started anyway.

so you probably want to make that an int datatype so you can go bigger, up to 65535mS.

65535 in an int? Not without a shoehorn.

0000 to FFFF, isn't that 65535?

0000 to FFFF, isn't that 65535?

Yes, but the range of values that fits in an int is -32768 to 32767. The range for an unsigned int is 0 to 65535.

Hey guys,

I'm realy confused now. I am a Design engineer, and i have work in a group. But my group members decided to give up, and now i have to do all the work alone. I am a real beginner in arduino, and thats why i contacted this forum. I updated the code acording to your suggestions, but i only recieve errors when i am trying to send it to my Arduino.

int sensor_button = 2;         
int transistor1 = 1;
int transistor2 = 11;
int transistor3 = 3;
int transistor4 = 4;
int transistor5 = 5;
int transistor6 = 6;
int transistor7 = 7;
int transistor8 = 8;
int transistor9 = 9;
int transistor10 = 10;

void setup() {
  pinMode(sensor_button, INPUT);      
  pinMode(transistor1, OUTPUT);   
  pinMode(transistor2, OUTPUT);  
 pinMode(transistor3, OUTPUT);  
 pinMode(transistor4, OUTPUT);  
 pinMode(transistor5, OUTPUT);  
 pinMode(transistor6, OUTPUT);  
 pinMode(transistor7, OUTPUT);  
 pinMode(transistor8, OUTPUT);  
 pinMode(transistor9, OUTPUT);  
 pinMode(transistor10, OUTPUT);   
}
byte pinMap [ 1,11, 3,4,5,6,7,8,9,10]; // note: 0,1 are used for serial comm's
byte onTime [3,6,9,12,15,18,21,24,27,30];  // transistor on times
byte sensor_count = 0;

if ( digitalRead (sensor_button) == HIGH ){
sensor_count = sensor_count +1;
digitalWrite ( pinMap [sensor_count] );
delay (onTime [sensor_count] );
}
}

  }
}

errors that i'm recieving are

sketch_jun13a:25: error: expected ]' before ',' token** **sketch_jun13a:25: error: expected unqualified-id before numeric constant** **sketch_jun13a:26: error: expected ]' before ',' token
sketch_jun13a:26: error: expected unqualified-id before numeric constant
sketch_jun13a:29: error: expected unqualified-id before 'if'
sketch_jun13a:34: error: expected declaration before '}' token

so i'm kind of Stuck, could you guys give me some more tips?

byte pinMap [ 1,11, 3,4,5,6,7,8,9,10];
byte pinMap [] = { 1,11, 3,4,5,6,7,8,9,10};

etc

Any comments to this new approach?

nt CountDown = 0; // De tijd die het duurt voor de buis draait (in milliseconden)
int OnTime = 3000; // De hoeveelheid milliseconden dat de zuiger uitschuift (relais aan)

int Catches = 0;
int Relays[] = {0,1,2,3,4,5,6,7,8};
int Tens = 0;
int ButtonPressed = 0;
int ButtOn = 0;
int SystemOn = 0;

void setup()
{
  pinMode (1, OUTPUT);
  pinMode (2, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (7, OUTPUT);
  pinMode (8, OUTPUT);

  pinMode (11, INPUT);
}

void loop()
{
  ButtonPressed = digitalRead(11);
  if (ButtonPressed == LOW && SystemOn == 0)
  {
    ButtOn = 1;
  }
  if (ButtonPressed == HIGH && ButtOn == 1)
  {
    Catches ++;
    ButtOn = 0;
    SystemOn = 1;
  }

  if (Catches == 10)
   { Catches = 0;
  }
  
  digitalWrite(Relays[Catches], HIGH);
  digitalWrite(Relays[Catches - 1], LOW);
  

  if(SystemOn == 1)
  {
    delay(CountDown);
    digitalWrite(10, LOW);
    delay(OnTime);
    digitalWrite(10, HIGH);
    delay(OnTime);
    SystemOn = 0;
  }

delay(1);
}
 digitalWrite(Relays[Catches - 1], LOW);

What if "Catches" is zero?

pinMode (1, OUTPUT);
  pinMode (2, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (7, OUTPUT);
  pinMode (8, OUTPUT);

You've already named them in an array; use a loop to do this.

AWOL:

 digitalWrite(Relays[Catches - 1], LOW);

What if "Catches" is zero?

when it reaches zero it will turn off all the relays, causing the system to start over, and give a signal to the unused pin 0

I asked someone from my class, and he's a real hero. He Just fixed everything and now it works

/* 
   !!!BELANGRIJK!!!
   ===========================================================
   Het 7-segmenten display wordt op poorten 1 t/m 7 geschakeld
   De 2 LEDs worden aangesloten op poort 8 & 9
   Het relais wordt op poort 10 aangesloten
   De sensor (drukplaat) wordt op poort 11 aangesloten
   ===========================================================
*/

int CountDown = 3000; // De tijd die het duurt voor de buis draait (in milliseconden)
int OnTime = 3000; // De hoeveelheid milliseconden dat de zuiger uitschuift (relais aan)

int Catches = 0;
int Relays[] = {0,1,2,3,4,5,6,7,8};
int Tens = 0;
int ButtonPressed = 0;
int ButtOn = 0;
int SystemOn = 0;

void setup()
{
  pinMode (1, OUTPUT);
  pinMode (2, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (7, OUTPUT);
  pinMode (8, OUTPUT);

  pinMode (11, INPUT);
}

void loop()
{
  ButtonPressed = digitalRead(11);
  if (ButtonPressed == LOW && SystemOn == 0)
  {
    ButtOn = 1;
  }
  if (ButtonPressed == HIGH && ButtOn == 1)
  {
    Catches ++;
    ButtOn = 0;
    SystemOn = 1;
  }

  if (Catches == 10)
   { Catches = 0;
  }
  


  if(SystemOn == 1)
  {
    digitalWrite(Relays[Catches], HIGH);
    delay(CountDown);
    digitalWrite(Relays[Catches], LOW);
    SystemOn = 0;
  }



  
delay(1);
}

Thanks for all the help!

digitalWrite(Relays[Catches - 1], LOW);

What if "Catches" is zero?
when it reaches zero it will turn off all the relays, causing the system to start over, and give a signal to the unused pin 0

No, I was referring to the possibility that you could turn off "Relays [-1]".
Now, C/C++ allows negative array indices (I once found it useful with pointers), but here, you don't have / can't have an array member with the index -1, so some "random" memory gets read, and digitalWrite attempts to write LOW to whatever that represents.

However, if you got it going, well done.

 pinMode (1, OUTPUT);
  pinMode (2, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (7, OUTPUT);
  pinMode (8, OUTPUT);

That still needs a for loop.