Skip to content

Instantly share code, notes, and snippets.

@KaiaKitten
Last active August 29, 2015 14:23
Show Gist options
  • Save KaiaKitten/bfa391bd03f184c11cc2 to your computer and use it in GitHub Desktop.
Save KaiaKitten/bfa391bd03f184c11cc2 to your computer and use it in GitHub Desktop.
/*
Exploring Arduino - Code Listing 6-6: Arduino Code to send Data to the Computer
http://www.exploringarduino.com/content/ch6
Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
*/
//Sending POT value to the computer
const int BUT1 =4;
const int BUT2 =2;
const int BUT3 =3;
const int POT=0; //Pot on Analog Pin 0
int val; //For holding mapped pot value
void setup()
{
pinMode(BUT1, INPUT);
pinMode(BUT2, INPUT);
pinMode(BUT3, INPUT);
Serial.begin(9600); //Start Serial
}
void loop()
{
val = map(analogRead(POT), 0, 1023, 0, 255); //Read and map POT
Serial.print((boolean)digitalRead(BUT1));//Send value
Serial.print(",");
Serial.print((boolean)digitalRead(BUT2));
Serial.print(",");
Serial.print((boolean)digitalRead(BUT3));
Serial.print(",");
Serial.println(val);
delay(50); //Delay so we don't flood the computer
}
/*
Exploring Arduino - Code Listing 6-7: Processing Code to Read Data and Change Color on the Screen
http://www.exploringarduino.com/content/ch6
Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
*/
//Processing Sketch to Read Value and Change Color on the Screen
//Import and initialize serial port library
import processing.serial.*;
import java.lang.Math;
Serial port;
String data = " ";
String[] values;
float brightness = 0; //For holding value from pot
float red = 0;
float green = 0;
float blue = 0;
void setup()
{
size(500,500); //Window size
port = new Serial(this, "/dev/ttyACM1", 9600); //Set up serial
port.bufferUntil('\n'); //Set up port to read until newline
}
void draw()
{
if(data != null)
{
values = data.split(",");
}
background(Float.parseFloat(values[0])*Float.parseFloat(values[3]),Float.parseFloat(values[1])*Float.parseFloat(values[3]),Float.parseFloat(values[2])*Float.parseFloat(values[3]));
}
void serialEvent (Serial port)
{
try{
data = port.readStringUntil('\n'); //Gets val
}catch(RuntimeException e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment