I'm trying to control a servo with arduino uno and C# but the servo doesn't move
Here is the Arduino code:
#include <Servo.h>
Servo servoT1;
void setup()
{
Serial.begin(9600);
servoT1.attach(9);
}
int i=0;
void loop()
{
if (Serial.available() > 0)
{
i=Serial.read();
i=map(i, 0, 100, 0, 179);
servoT1.write(i);
}
}
Here is the C# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace arduino_throttle
{
public partial class Form1 : Form
{
SerialPort uno1 = new SerialPort("COM3", 9600);
public Form1()
{
InitializeComponent();
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
uno1.Open();
uno1.Write(HScroll.ToString());
uno1.Close();
}
}
}
I want to set the servo angle by scrolling the hScrollBar in the program.
the serial.read () method return the first byte of incoming serial data available
http://arduino.cc/en/Serial/Read
but you are sending string (array of chars)
try sending from the C# only one byte or char
to do this you need to check the max value is 256
and another thing
you need to send the value of the scrollbar
(you are sending the entire object in a string)
try this
uno1.Write(Convert.ToByte(HScroll.value));
Related
I have the problem that when I send data (AT-Commands) to my Dial-Up Modem my program doesnt respond any more and crashes. I use System.IO.Ports to make this work. This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace Windows_XP_AOL
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
SerialPort serialPort = new SerialPort("COM1" , 9600);
serialPort.ReadTimeout = 10000;
serialPort.WriteTimeout = 10000;
serialPort.Open();
serialPort.DtrEnable = true;
serialPort.WriteLine("ATA");
serialPort.ReadLine();
serialPort.Close();
}
}
}
On the modem the Send Data Light lights up and the Read Data Light. But when I click on my button that sends the AT-Command (HAYES) (serialPort.WriteLine("ATA")) then my program just doesn't respond anymore and crashes. Because of my timeout function, it doesn't crash but my modem doesn't execute the command that I'm sending. Does someone know what I am making wrong? I can try to explain with pictures if someone doesn't understand it. Please I need this to work! Thanks for any help :)
I have this code that uses NAudio to look for all microphones connected on laptop, andwhen I choose one it shows a meter of the sound in a progressbar.
when I gather all device, I collect them in a dropdwon list, my issue is that when I choose the one microphone from the list it will not be activated and display the sound on a meter unless I start WINDOWS SOUND RECORDER which seems to activate the mic.
how to enable or activate the microphone from the code without starting WINDOWS SOUND RECORDER?
here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio;
using NAudio.CoreAudioApi;
using System.Threading;
using NAudio.Wave;
using NAudio.Mixer;
namespace NaudioVisualizer
{
public partial class Form1 : Form
{
public int a = 1;
public Form1()
{
InitializeComponent();
MMDeviceEnumerator de = new MMDeviceEnumerator();
var device = de.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
comboBox1.Items.AddRange(device.ToArray());
}
private void timer1_Tick(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
var device = (MMDevice)comboBox1.SelectedItem;
progressBar1.Value = (int)Math.Round(device.AudioMeterInformation.MasterPeakValue * 100);
}
}
}
}
I had the same problem try to use the following code :
var waveIn = new WaveIn();
waveIn.StartRecording();
I followed a very basic tutorial (forgot the link) and it all seems straightforward, but I don't seem to be getting the output I want. Here is my main form class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace OralNotes.Alpha
{
public partial class MainForm : Form
{
private SpeechRecognitionEngine recognitionEngine;
public MainForm()
{
InitializeComponent();
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.8f)
txtNotes.Text += word.Text + " ";
}
txtNotes.Text += Environment.NewLine;
};
recognitionEngine.LoadGrammar(new DictationGrammar());
}
private void btnStart_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsync();
}
private void btnStop_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}
}
}
And you probably don't need this, but here it is anyway:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace OralNotes.Alpha
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
I click start and talk, and nothing happens.
UPDATE*
ok so this is working, if I adjust this line
if (word.Confidence > 0.8f)
to a lower number I get a word or two in. Not transcribing sentences or anything, is there a way to make this have better recognition?
It seems that the engine that you are using returns a confidence value on every word it hears.
I suspect that the confidence values range from 0-1. Which means that if you lower the value from 0.8 you are making the engine more tolerant to words. Maybe it is the quality of your mic?
I want to be able to get input from a microphone device via NAudio.WaveIn, and then output that exact input to an output device via NAudio.WaveOut.
How would I do this?
Here is the code that worked for me:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.CoreAudioApi;
namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
private BufferedWaveProvider bwp;
WaveIn wi;
WaveOut wo;
public Form4()
{
InitializeComponent();
wo = new WaveOut();
wi = new WaveIn();
wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);
bwp = new BufferedWaveProvider(wi.WaveFormat);
bwp.DiscardOnBufferOverflow = true;
wo.Init(bwp);
wi.StartRecording();
wo.Play();
}
void wi_DataAvailable(object sender, WaveInEventArgs e)
{
bwp.AddSamples(e.Buffer, 0, e.BytesRecorded);
}
}
}
The best way would be to use a BufferedWaveProvider as the input to WaveOut. Then in the DataAvailable callback of WaveIn, supply the data recorded to the BufferedWaveProvider
void DataAvailable(object sender, WaveInEventArgs args)
{
bufferedWaveProvider.AddSamples(args.Buffer, 0, args.BytesRecorded);
}
You need to be aware that the default buffer sizes will result in a noticeable delay, so if you were hoping for low latency you might need to experiment a bit with buffer sizes to see how low you can get it.
I am getting an unhandled exception when trying to create a D3d device.
This is literally the whole code in C#. I am just testing and trying to learn directx.
I am getting the Unhandled exception on this line
: device = new Device(0, DeviceType.Hardware, this,CreateFlags.SoftwareVertexProcessing, presentParams);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Device device = null; // Create rendering device
PresentParameters presentParams = new PresentParameters();
device = new Device(0, DeviceType.Hardware, this,CreateFlags.SoftwareVertexProcessing, presentParams);
RenderToSurface render = new RenderToSurface(device, 300, 300, Format.D32, true, DepthFormat.D32);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Kind of blowing my mind because it looks like all the params are right..
needed presentParams.Windowed = true in order for this to work.