Use Arduino signals as input for PC?

I recently got hands from Arduino (Uno), and I was interested in something.

I do not have an external volume control for my speakers, so I thought it might be possible to connect the potentiometer to the Arduino and then use it to control the volume (on Windows). But is it possible?

To read the value of an Arduino pin using, possibly, Visual C, C ++ (or another “multi-platform” language)? And then use this to set the volume level on Windows (and, if possible, also on Linux).

I thought this was possible because if you use:

Serial.println(analogRead([pin with potentiometer]));

You can get the potentiometer values ​​on the PC (via USB). So is it possible to read these values ​​in C or C ++?

I know how to install a volume on Windows using C or C ++, I only need to know if there is a way to read the potentiometer values ​​in a (Visual) C or C ++ script.

+5
source share
2 answers

Of course. And using exactly the same method: serial communication. Since I'm not a big Windows expert, I can't write you a complete Windows example, but here are some snippets that can run you on Unix (Linux, OS X, etc.):

Arduino Code:

#define POT_PIN 1

void setup()
{
    Serial.begin(9600); // 9600 baud is more than enough
}

void loop()
{
    int pot = analogRead(POT_PIN);
    unsigned char byte = (pot + 2) >> 2; // round and divide by 4
    Serial.write(pot);
    delay(100); // or do something useful
}

Computer code:

#include <termios.h>

struct termios tio;
memset(&tio, 0, sizeof(tio));

// Open serial port in mode `8N1', non-blocking
tio.c_cflag = CS8 | CREAD | CLOCAL;
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 5;

int fd = open(device, O_RDONLY);

cfsetospeed(&tio, B9600);
cfsetispeed(&tio, B9600);
tcsetattr(fd, TCSANOW, &tio);

while (1) {
    unsigned char byte;
    read(fd, &byte, 1);
    use_some_library_to_set_volume(byte);
}
+6
source

There are several things to consider.

  • , " Arduino". , USB, . , . , , "123 128 133 145", .

  • Serial.whatever, Arduino, , UART. Arduino, , , RS-232 USB-. , RS-232 9- d-sub . , RS-232 USB.

  • , COM- . USB RS-232 , . , , .

  • API Windows . MSDN.

0

All Articles