Advertisement
VinSS

Servo_0.2

Nov 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo servo1;
  4.  
  5. void setup() {
  6. // put your setup code here, to run once:
  7. servo1.attach(6);
  8. Serial.begin(9600);
  9. Serial.println("Start");
  10. }
  11.  
  12. void loop() {
  13.  
  14. if (Serial.available() > 0){
  15. int i = Serial.parseInt();
  16. Serial.println(i);
  17. servo1.write(i);
  18. }
  19. }
  20.  
  21. ---------------------------------------------------------------------
  22.  
  23. using System;
  24. using System.Collections.Generic;
  25. using System.ComponentModel;
  26. using System.Data;
  27. using System.Drawing;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Threading.Tasks;
  31. using System.Windows.Forms;
  32.  
  33. using System.IO.Ports;
  34.  
  35. namespace WindowsFormsApplication3
  36. {
  37. public partial class Form1 : Form
  38. {
  39. private delegate void SafeCallDelegate(string text);
  40.  
  41. public Form1()
  42. {
  43. InitializeComponent();
  44.  
  45. comboBox1.Items.Clear();
  46. foreach (string portName in SerialPort.GetPortNames())
  47. {
  48. comboBox1.Items.Add(portName);
  49. }
  50. comboBox1.SelectedIndex = 0;
  51. }
  52.  
  53. private void button2_Click(object sender, EventArgs e)
  54. {
  55. serialPort1.Open();
  56. }
  57.  
  58. private void button3_Click(object sender, EventArgs e)
  59. {
  60. serialPort1.Close();
  61. }
  62.  
  63. private void button1_Click(object sender, EventArgs e)
  64. {
  65. if (serialPort1.IsOpen)
  66. {
  67. try
  68. {
  69. serialPort1.Write(textBox1.Text);
  70. }
  71. catch (Exception ex)
  72. {
  73. MessageBox.Show(ex.Message);
  74. }
  75. }
  76. }
  77.  
  78. private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  79. {
  80. SerialPort sp = (SerialPort)sender;
  81. WriteTextSafe(sp.ReadExisting());
  82. }
  83.  
  84. private void WriteTextSafe(string text)
  85. {
  86. if (label1.InvokeRequired)
  87. {
  88. var d = new SafeCallDelegate(WriteTextSafe);
  89. label1.Invoke(d, new object[] { text });
  90. }
  91. else
  92. {
  93. label1.Text = text;
  94. }
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement