Arduino - servomotor

 
Vista:

servomotor

Publicado por augusto (1 intervención) el 13/02/2019 17:42:57
ayuda quiero programar arduino y controlar un servomotor copie un codigo de Internet de secuensia de 0 a 180 pero mi servo solo gira en el mismo sentido solo que disminuya la velocidad que puedo hacer
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 63
Oro
Ha mantenido su posición en Arduino (en relación al último mes)
Gráfica de Arduino

servomotor

Publicado por Meta (27 intervenciones) el 16/02/2019 10:41:21
Hola:

Deja ver mi ejemplo que hice hace tiempo si lo encuentro, te lo paso.

Espera...

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 63
Oro
Ha mantenido su posición en Arduino (en relación al último mes)
Gráfica de Arduino

servomotor

Publicado por Meta (27 intervenciones) el 16/02/2019 11:13:31
Buenas:

Código de Arduino:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <Servo.h>
//Creamos una variable servo para poder usar las funciones con ella.
Servo miservo;
int valor = 0;
int angulo;
int x;
 
void setup()
{
  Serial.begin(115200); // Velocidad en baudios y configuraciçon de inicio.
  //Definimos el pin al que ira conectado el servo.
  miservo.attach(6); Como salida, el pin 6 con uso de PWM.
  //Movemos el servo al centro
  miservo.write(90);  // Coloca el servo en su posición central.
}
void loop() {
 
 
  if (Serial.available() > 0) // Espera entrada de datos de 0 a 180.
  {
    valor = Serial.read(); // Los datos leidos se almacena en la variable valor.
    if (valor > 0)
    {
      x = valor;
    }
  }
  else if ((x >= 0) && (x <= 180))
  {
    angulo = x;
    miservo.write(angulo); // Envía los comando finales de 0 a 180 al servo.
  }
 
 
 
  /*
    long angulo;
    if (Serial.available() > 0)
    {
      angulo = Serial.parseInt();
      if ((angulo >= 0) && (angulo <= 180))
      {
        miservo.write(angulo);
        delay(15);
      }
    }
  */
}

servo-2001560

Código de C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports; // No olvidar.
using System.Drawing;
using System.Drawing.Drawing2D;
 
namespace ServoArduino_01_cs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button_0_Click(object sender, EventArgs e)
        {
            int pasos_0;
            int.TryParse(button_0.Tag.ToString(), out pasos_0);
            trackBar1.Value = pasos_0;
        }
 
        private void button_45_Click(object sender, EventArgs e)
        {
            int pasos_45;
            int.TryParse(button_45.Tag.ToString(), out pasos_45);
            trackBar1.Value = pasos_45;
        }
 
        private void button_90_Click(object sender, EventArgs e)
        {
            int pasos_90;
            int.TryParse(button_90.Tag.ToString(), out pasos_90);
            trackBar1.Value = pasos_90;
        }
 
        private void button_135_Click(object sender, EventArgs e)
        {
            int pasos_135;
            int.TryParse(button_135.Tag.ToString(), out pasos_135);
            trackBar1.Value = pasos_135;
        }
 
        private void button_180_Click(object sender, EventArgs e)
        {
            int pasos_180;
            int.TryParse(button_180.Tag.ToString(), out pasos_180);
            trackBar1.Value = pasos_180;
        }
 
        #region Conectar puerto.
        private void Form1_Load(object sender, EventArgs e)
        {
            button_0.Tag = 0;
            button_45.Tag = 45;
            button_90.Tag = 90;
            button_135.Tag = 135;
            button_180.Tag = 180;
 
            // Añado los puertos disponible en el PC con SerialPort.GetPortNames() al comboBox_Puerto.
            try
            {
                comboBox_Puerto.DataSource = SerialPort.GetPortNames();
            }
 
            catch
            {
                MessageBox.Show("No encuentra ningún puerto físico ni virtual.", "Aviso:",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
 
            // Añado los puertos disponible en el PC con SerialPort.GetPortNames() al comboBox_Puerto.
            comboBox_Puerto.DataSource = SerialPort.GetPortNames();
 
            // // Añade puertos disponibles físicos  y virtuales.
            serialPort1.PortName = comboBox_Puerto.Text.ToString();
        }
 
        // Detecta USB o puerto serie virtual cuando lo conecta y desconecta del cable.
        protected override void WndProc(ref Message USB)
        {
            if (USB.Msg == 0x219)
            {
                comboBox_Puerto.DataSource = SerialPort.GetPortNames();
            }
 
            base.WndProc(ref USB); // Detecta si hay cambios en el usb y si los hay los refleja.
        }
 
        private void button_Conectar_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = comboBox_Puerto.Text.ToString(); // Puerto seleccionado previamente.
                serialPort1.Open(); // Abrir puerto.
                comboBox_Puerto.Enabled = false;
                button_Conectar.Enabled = false;
                button_Desconectar.Enabled = true;
                // Botones grados.
                button_0.Enabled = true;
                button_45.Enabled = true;
                button_90.Enabled = true;
                button_135.Enabled = true;
                button_180.Enabled = true;
                trackBar1.Enabled = true;
                progressBar1.Enabled = true;
            }
 
            catch
            {
                MessageBox.Show("El puerto no existe.", "Aviso:",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
 
        private void button_Desconectar_Click(object sender, EventArgs e)
        {
            serialPort1.Close(); // Cerrar puerto.
            comboBox_Puerto.Enabled = true;
            button_Conectar.Enabled = true;
            button_Desconectar.Enabled = false;
            // Botones grados.
            button_0.Enabled = false;
            button_45.Enabled = false;
            button_90.Enabled = false;
            button_135.Enabled = false;
            button_180.Enabled = false;
            trackBar1.Enabled = false;
            progressBar1.Enabled = false;
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) // ¿El puerto está abierto?
            {
                serialPort1.Close(); // Cerrar puerto.
            }
        }
        #endregion
 
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            string valor = trackBar1.Value.ToString();
            byte[] mBuffer = Encoding.ASCII.GetBytes(valor);
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }
 
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            // Crear brocha sólida.
            SolidBrush fondoNegro = new SolidBrush(Color.Black);
 
            // Crear localización y tamaño del eclipse.
            int x = 0;
            int y = 0;
            int anchura = 100;
            int altura = 100;
 
            // Dibujamos un círculo negro.
            e.Graphics.FillEllipse(fondoNegro, x, y, anchura, altura);
 
            Pen pen = new Pen(Color.Gray, 8);
            pen.StartCap = LineCap.ArrowAnchor;
            pen.EndCap = LineCap.RoundAnchor;
            e.Graphics.DrawLine(pen, 50, 0, 50, 50);
        }
    }
}

Que se sirva de ayuda a ti y otros visitantes.

Un cordial saludos.

PD: Haré más tutoriales sobre este tema en PDF.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar