how to edit the script

Hello, I am a bit lost in the topic of programming, i need to be able modify this code

tone (10, map (value, 0,1250,55,423)

to use a servo in this.

greetings

i need to be able modify this code

tone (10, map (value, 0,1250,55,423)

to use a servo in this.

What do you want the servo to do? Where does value come from? I'm not familiar with any C statements that allow a different number of ( and ) or that do not end with a semicolon.

ooh okey I want to do a speedometer, this information is of the game omsi

Search this forum for OMSI and you will find other bus simulator fans.

peter-salvador:
i need to be able modify this code

tone (10, map (value, 0,1250,55,423));

to use a servo in this.

I added a bit in red to make that look like valid C++.

What that does is take value and map it from one range to another, and use the resulting value as the frequency argument in a call to tone().

Do you want to set the position of a servo instead of outputting the tone? If so you could create and attach a Servo object and call its write() method to request the servo to move to a specified position. You can calculate that position from value if you want.

i found this code

/*
Añadí toda las linea de comentario
el codigo lo explico para los que no entienden muy bien como “funciona” y que es cada “cosa”, un buen codigo sin sus comentarios, no es un codigo completo :wink:
Comentarios del codigo por : Alfonso carmona
*/
//Seccion de includes
#include <Servo.h>; //incluimos la libreria Servos.h

//Definimos las variables que vamos a utilizar de forma “Global”
int rpm; //variable tipo int
int Speed; //variable tipo int
Servo Speedserv; //variable de la clase Servo (obtiene las propiedades de la clase)
char comandos; // variable tipo char “caracter”
int serv = false; //variable tipo int, en otros lenguajes podriamos decir que seria una variable de tipo booleam ( true o false)

//Funcion “setup”, esta funcion es la que inicia las “opciones” de arduino, tales como los pines que queremos de “entrada”, “salida”, parametros a definir…etc…
void setup(){
Speedserv.attach(9); //igual que el anterior

serv = true; //cambiamos el valor de la variable serv, a true
} //cerramos la sentencia if

while(Serial.available() > 0) // sentencia “while”( quiere decir “mientras”), en este caso, “mientras”, Serial.available() no sea igual a “0” ( podriamos decir, que el while se ejecutara siempre que contenga datos
{

comandos = Serial.read(); // lee un caracter “char” y lo guarda en la variable comandos
if (comandos == ‘S’ ) Read_Speed(); //sentencia “if”, si la variable “comandos” es igual “==” a “S”, ejecuta la funcion Read_Speed();
} //fin de la sentencia “while”

void Read_Speed() //funcion Read_Speed() esta funcion es la encargada de recoger los datos de “Speed”,( la velocidad) y lo procesa
{
delay(2); //delay de 2 milesimas
int Speed100 = Serial.read() - 48; // lee el primer caracter “char”
delay(2); //delay, ya lo explicamos, paramos el proceso durtante 2 milisegundo
int Speed2 = Serial.read() - 48;// lee el segundo caracter “char”
delay(2);//delay, ya lo explicamos, paramos el proceso durtante 2 milisegundo
int Speed1 = Serial.read()- 48;// lee el tercer caracter “char”
delay(2);//delay, ya lo explicamos, paramos el proceso durtante 2 milisegundo
int Speed = 100Speed100 + 10Speed2 + Speed1; //hacemos un calculo con los datos recogidos, y los guardamos en la variable “Speed”
int cal2 = map(Speed, 0, 200, 170, 0); //funcion o metodo “map” los parametros para esta funcion o metodo son : map(Valor, Valor_minimo, valor_maximo, Servo_minimo, Servo_maximo), el resultado de dicha funcion o metodo, es calcular, en que posicion tiene que estar el “servo”, calculando el “valor maximo que recibira” por el valor maximo del servo “175 grados en nuestro caso” para saber mas de esta funcion o metodo, busquen en la web de arduino
Speedserv.write(cal2); // con la funcion o metodo write, añadimos el resultado que nos dio la funcion o metodo “map”( dando por resultado un valor de 0 a 170 grados)
} //fin de la funcion Read_Speed()

How can I use this code in omsi???

How can I use this code in omsi???

What makes you think you can? What problems are you having doing so?

There seems to be a real minority here that knows what you mean by omsi. If you want real help, you'll define what omsi is.

okey omsi is a bus simulator

http://www.omnibussimulator.de/english.htm

I want to use servos to use as gauges (speed,water, oil pressure etc)

i have this code

// Last updated: March 15th, 2012 - 8:40PM GMT
// Using state machine pattern - based on Nick Gammon code

typedef enum { NONE, GOT_I, GOT_O, GOT_R, GOT_o, GOT_T, GOT_S, GOT_B, GOT_H, GOT_b, GOT_h, GOT_P, GOT_F, GOT_A, GOT_W, GOT_V, GOT_w, GOT_r, GOT_k, GOT_a } states;
states state = NONE;
unsigned int currentValue;

void setup ()
{
Serial.begin (115200);
TCCR4B = (TCCR4B & 0xF8) | 0x01;
state = NONE;
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(28, OUTPUT);
pinMode(29, OUTPUT);
pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
pinMode(34, OUTPUT);
}

// Lights

void Read_BusStopLight(const unsigned int value)
{
if (value == 1)
{
digitalWrite(22,HIGH);
}
else
{
digitalWrite(22,LOW);
}
}

void Read_Blinker(const unsigned int value)
{
if (value == 1)
{
digitalWrite(24,HIGH);
}
else
{
digitalWrite(24,LOW);
}
}

void Read_Highbeam(const unsigned int value)
{
if (value == 1)
{
digitalWrite(25,HIGH);
}
else
{
digitalWrite(25,LOW);
}
}

void Read_Battery(const unsigned int value)
{
if (value == 1)
{
digitalWrite(26,HIGH);
}
else
{
digitalWrite(26,LOW);
}
}

void Read_Handbrake(const unsigned int value)
{
if (value == 1)
{
digitalWrite(27,HIGH);
}
else
{
digitalWrite(27,LOW);
}
}

void Read_LowPressure(const unsigned int value)
{
if (value == 1)
{
digitalWrite(28,HIGH);
}
else
{
digitalWrite(28,LOW);
}
}

void Read_ABS(const unsigned int value)
{
if (value == 1)
{
digitalWrite(29,HIGH);
}
else
{
digitalWrite(29,LOW);
}
}

void Read_RearDoor(const unsigned int value)
{
if (value == 1)
{
digitalWrite(30,HIGH);
}
else
{
digitalWrite(30,LOW);
}
}

void Read_RetarderDirect(const unsigned int value)
{
if (value == 1)
{
digitalWrite(31,HIGH);
}
else
{
digitalWrite(31,LOW);
}
}

void Read_FourFlasher(const unsigned int value)
{
if (value == 1)
{
digitalWrite(32,HIGH);
}
else
{
digitalWrite(32,LOW);
}
}

void Read_Backlight(const unsigned int value)
{
if (value == 1)
{
digitalWrite(33,HIGH);
}
else
{
digitalWrite(33,LOW);
}
}

void Read_ASR(const unsigned int value)
{
if (value == 1)
{
digitalWrite(34,HIGH);
}
else
{
digitalWrite(34,LOW);
}
}

// Gauges

void Read_Temperature(const unsigned int value)
{
analogWrite(7,map(value,0,100,255,0));
}

void Read_Rpm(const unsigned int value)
{
tone(9, map(value,0,5700,55,423));
}

void Read_Oil(const unsigned int value)
{
analogWrite(8,map(value,0,50,0,255));
}

void Read_Fuel(const unsigned int value)
{
analogWrite(6,map(value,0,100,255,0));
}

void Read_Velocity(const unsigned int value)
{
tone(10, map(value,0,1250,55,423));
}

void handlePreviousState()
{
switch (state)
{
case GOT_R:
Read_Rpm(currentValue);
break;
case GOT_o:
Read_Oil(currentValue);
break;
case GOT_T:
Read_Temperature(currentValue);
break;
case GOT_S:
Read_BusStopLight(currentValue);
break;
case GOT_B:
Read_Blinker(currentValue);
break;
case GOT_H:
Read_Highbeam(currentValue);
break;
case GOT_b:
Read_Battery(currentValue);
break;
case GOT_h:
Read_Handbrake(currentValue);
break;
case GOT_P:
Read_LowPressure(currentValue);
break;
case GOT_F:
Read_Fuel(currentValue);
break;
case GOT_A:
Read_ABS(currentValue);
break;
case GOT_W:
Read_RearDoor(currentValue);
break;
case GOT_V:
Read_Velocity(currentValue);
break;
case GOT_w:
Read_FourFlasher(currentValue);
break;
case GOT_r:
Read_RetarderDirect(currentValue);
break;
case GOT_k:
Read_Backlight(currentValue);
break;
case GOT_a:
Read_ASR(currentValue);
break;
case GOT_I:
digitalWrite(23,HIGH);
break;
case GOT_O:
digitalWrite(23,LOW);
break;
}
currentValue = 0;
}

void processIncomingByte (const byte c)
{
if (isdigit (c))
{
currentValue *= 10;
currentValue += c - '0';
}
else
{

handlePreviousState ();
switch (c)
{
case 'I':
state = GOT_I;
break;
case 'O':
state = GOT_O;
break;
case 'R':
state = GOT_R;
break;
case 'o':
state = GOT_o;
break;
case 'T':
state = GOT_T;
break;
case 'S':
state = GOT_S;
break;
case 'B':
state = GOT_B;
break;
case 'H':
state = GOT_H;
break;
case 'b':
state = GOT_b;
break;
case 'h':
state = GOT_h;
break;
case 'P':
state = GOT_P;
break;
case 'F':
state = GOT_F;
break;
case 'A':
state = GOT_A;
break;
case 'W':
state = GOT_W;
break;
case 'V':
state = GOT_V;
break;
case 'w':
state = GOT_w;
break;
case 'r':
state = GOT_r;
break;
case 'k':
state = GOT_k;
break;
case 'a':
state = GOT_a;
break;
default:
state = NONE;
break;
}
}

}

void loop()
{
if (Serial.available())
processIncomingByte (Serial.read());
}

in gauges section are configure to play a tone and servos has other configuration but i dont know which is

Please, for the love of god, learn how to post code.

We have "code" tags - use them.

How to use this forum - please read.

ohh okey sorry but this code is about the problem

peter-salvador:
ohh okey sorry but this code is about the problem

And if you want help with that problem, follow directions.

yeah , okey

void Read_BusStopLight(const unsigned int value)
{
  if (value == 1)
  {
        digitalWrite(22,HIGH);
  }
  else
  {
        digitalWrite(22,LOW);
  }
}

This function, like a number of others doesn't read a damned thing. HIGH is 1. LOW is not. This code could be much shorter:

void Read_BusStopLight(const unsigned int value)
{
    digitalWrite(22, value;
}

Of course, it still doesn't read anything.

And, you STILL haven't explained what the problem is.

I get the impression that pin numbers are wrong and or inconsistently stated with at leas one (possibly two) "Read_" routines mislabeled and or missing completely.

my problem is that this code tone (10, map (value, 0,1250,55,423) is for a tone not for a servo.

My problem is that you seem to not have a clue what you want to do. A servo and a piezo have very different characteristics. They are not interchangeable. The value in that mangled function call comes from somewhere. Where? How do you expect that value to relate to a servo position?

peter-salvador:
Hello, I am a bit lost in the topic of programming, i need to be able modify this code

tone (10, map (value, 0,1250,55,423)

to use a servo in this.

greetings

peter-salvador,

A bit lost you say!

Really just a bit?

Peter is English your native language.