# Using UNO script to OPTA

**URL:** <https://forum.arduino.cc/t/using-uno-script-to-opta/1240468>\
**Category:** Opta\
**Created:** [March 27, 2024, 8:30am UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468 "2024-03-27T08:30:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredo37](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@fredo37](https://forum.arduino.cc/u/fredo37)\
**Post date:** [March 27, 2024, 8:30am UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/1 "2024-03-27T08:30:04Z")

</div>

Hello everyone

I have a question about the Opta arduino; Can a script initially created for a UNO work with OPTA? (of course changing the names of the inputs and outputs)?

because I tried my script which works on UNO on OPTA and nothing happens...

I used ARDUINO IDE 2.3.2

```arduino

unsigned long tempsDebut; // passage précédent de l'aimant

int etatPrecedent; // lors de du dernier passage dans le loop,
                    
const int rel_0 = D0;

// niveau du seuil de vitesse (km/h) pour le déclenchement de la sortie output 7

const int seuil5 = 3000;

void setup() {
  pinMode(A0, INPUT); // interrupteur Reed à la pin 8
  pinMode(rel_0, OUTPUT); // configure la broche ledPin en sortie
  
  pinMode(LED_D0, OUTPUT);
  
  Serial.begin(9600); // moniteur série
  etatPrecedent = digitalRead(A0); // on vérifie si l'aimant est là ou pas
}

void loop() {

  int etat, delai;
  unsigned long tempsFin;
  int valeurCapteur = digitalRead(A0);
  etat = digitalRead(A0); // on vérifie si l'aimant est là ou pas
  
  if ((etat) && (!etatPrecedent)) { // l'aimant vient d'arriver
    if (tempsDebut > 0) { // nous connaissons le moment du début de ce tour
      tempsFin = millis();            
      delai = tempsFin - tempsDebut; // durée du tour qui vient de se terminer

      if (delai <= seuil5)  
      {
        digitalWrite(rel_0, HIGH);
        digitalWrite(LED_D0,HIGH);
      } 
      else {
        digitalWrite(rel_0, LOW);
        digitalWrite(LED_D0,LOW);
      }
      
    }

    tempsDebut = tempsFin; // mise à jour de la variable
  }

  etatPrecedent = etat; // mise à jour de la variable
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 27, 2024, 8:39am UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/2 "2024-03-27T08:39:53Z")

</div>

here is the pinout

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/2/7/c/27c695ed697e0936430ec1f5b997bffafee5dc15.png)

show us your circuit and what was connected where and how things are powered.

PS/ You need a ` while (!Serial);` after your `Serial.begin(9600);` if you want to print anything out quickly but that's not your issue here

---

<div class="post-metadata">

**Author:** ![fredo37](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@fredo37](https://forum.arduino.cc/u/fredo37)\
**Post date:** [March 27, 2024, 10:01am UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/3 "2024-03-27T10:01:44Z")

</div>

here is the script for the tests.  
I don't use relays.  
I just want to turn on or off the D0 LED, if it works I will put the relays on.

```arduino
unsigned long tempsDebut; // passage précédent de l'aimant

int etatPrecedent; // lors de du dernier passage dans le loop,
                    
//const int rel_0 = D0; //not useful for testing

const int seuil5 = 3000;

void setup() {
  pinMode(BTN_USER, INPUT); // user button for testing
  //pinMode(rel_0, OUTPUT); // not useful for testing
  pinMode(LED_D0, OUTPUT);
  
  etatPrecedent = digitalRead(BTN_USER); // on vérifie si l'aimant est là ou pas
}

void loop() {

  int etat, delai;
  unsigned long tempsFin;
  int valeurCapteur = digitalRead(BTN_USER);
  etat = digitalRead(BTN_USER); // on vérifie si l'aimant est là ou pas
  
  if ((etat) && (!etatPrecedent)) { // l'aimant vient d'arriver
    if (tempsDebut > 0) { // nous connaissons le moment du début de ce tour
      tempsFin = millis();            
      delai = tempsFin - tempsDebut; // durée du tour qui vient de se terminer

      if (delai <= seuil5)  
      {
        //digitalWrite(rel_0, HIGH); //not useful for testing
        digitalWrite(LED_D0,HIGH);
      } 
      else {
        //digitalWrite(rel_0, LOW); //not useful for testing
        digitalWrite(LED_D0,LOW);
      }
      
    }

    tempsDebut = tempsFin; // mise à jour de la variable
  }

  etatPrecedent = etat; // mise à jour de la variable
}

```

---

<div class="post-metadata">

**Author:** ![fredo37](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@fredo37](https://forum.arduino.cc/u/fredo37)\
**Post date:** [March 27, 2024, 10:03am UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/4 "2024-03-27T10:03:18Z")

</div>

I use the BTN\_USER to simulate a proximity sensor

---

<div class="post-metadata">

**Author:** ![aubrey4485](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@aubrey4485](https://forum.arduino.cc/u/aubrey4485)\
**Post date:** [March 27, 2024, 1:40pm UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/5 "2024-03-27T13:40:54Z")

</div>

I am not a great programmer and i don’t understand local variables. I always use global variables cause I understand doing it this way. What comes to my mind is

> [@fredo37](#):
>
> `LED_D0`

Has never been declared to a pin maybe? But I can be way off on this…

etat & etatPrecedent are supposed to be Boolean’s i believe ,,, i do not think this will behave in the way you suspect.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 27, 2024, 1:54pm UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/6 "2024-03-27T13:54:42Z")

</div>

> [@aubrey4485](#):
>
> What comes to my mind is
> 
> > [@fredo37](#):
> >
> > `LED_D0`
> 
> Has never been declared to a pin maybe? But I can be way off on this…

this is part of that board's environment

![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/a/f/9afe71259833443d042a15b07aab607088e1e7fb.png)

* * *

> [@aubrey4485](#):
>
> etat & etatPrecedent are supposed to be Boolean’s i believe ,,, i do not think this will behave in the way you suspect.

its poorly written (should compare with HIGH and LOW rather than treat those as implicit 1 and 0) but should work

---

<div class="post-metadata">

**Author:** ![aubrey4485](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@aubrey4485](https://forum.arduino.cc/u/aubrey4485)\
**Post date:** [March 27, 2024, 2:39pm UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/7 "2024-03-27T14:39:29Z")

</div>

Gotcha 👍🏼… 1 is true/HIGH and 0 is false/LOW, i see. Did not know that would work. thanks! Hey OP, listen to this guy ^ … lol.

I guess when you say it is part of the environment. There is no other option so it (what pin) does not need to be declared. Correct?

---

<div class="post-metadata">

**Author:** ![fredo37](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@fredo37](https://forum.arduino.cc/u/fredo37)\
**Post date:** [March 27, 2024, 4:44pm UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/8 "2024-03-27T16:44:33Z")

</div>

thank you friends .  
for my part I have just tested my script by removing the line

```arduino
if (tempsDebut > 0) { }

```

and it works.... 🤔

my opta is powered by 12v,  
then I connect the + of the opta to an input I1,I2,I3.....

what is the maximum admissible voltage on an input, 10v or 24v

---

<div class="post-metadata">

**Author:** ![johnnycatt](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@johnnycatt](https://forum.arduino.cc/u/johnnycatt)\
**Post date:** [March 31, 2024, 4:24pm UTC](https://forum.arduino.cc/t/using-uno-script-to-opta/1240468/9 "2024-03-31T16:24:36Z")

</div>

The Datasheet says Analog Input has a 10v maximum, but I'm running my as "digital" and they work fine with a 12v input. The Datasheet also says the Digital Input pins can handle up to 24V, but according to the pin-out, there are no "digital inputs," so, I'm a little confused by what they mean...  
They may be talking about the 12-24v input from the power supply, but that doesn't work for me as the "digital Input" says a power range of: "0v...24v," which emplys a 2v input would be fine...

The datasheet also says the OPTA has "over-volt protection," on each pin, so you can (theoretically) use any voltage and not destroy the machine - it may not work, but you won't kill it by trying.
