https://interactionstation.wdka.hro.nl/mediawiki/index.php?title=Special:NewPages&feed=atom&limit=50&offset=&namespace=0&username=&tagfilter=Interaction Station Wiki - New pages [en]2024-03-29T06:12:35ZFrom Interaction Station WikiMediaWiki 1.35.7https://interactionstation.wdka.hro.nl/wiki/HC-SR04_Ultrasonic_sensorHC-SR04 Ultrasonic sensor2024-03-22T09:27:55Z<p>Annasa: </p>
<hr />
<div>==Basic code==<br />
<syntaxhighlight lang="c"> <br />
// defines pins numbers<br />
const int trigPin = 2;<br />
const int echoPin = 3;<br />
// defines variables<br />
long duration;<br />
int distance;<br />
<br />
void setup() {<br />
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output<br />
pinMode(echoPin, INPUT); // Sets the echoPin as an Input<br />
Serial.begin(9600); // Starts the serial communication<br />
}<br />
void loop() {<br />
// Clears the trigPin<br />
digitalWrite(trigPin, LOW);<br />
delayMicroseconds(2);<br />
// Sets the trigPin on HIGH state for 10 micro seconds<br />
digitalWrite(trigPin, HIGH);<br />
delayMicroseconds(10);<br />
digitalWrite(trigPin, LOW);<br />
// Reads the echoPin, returns the sound wave travel time in microseconds<br />
duration = pulseIn(echoPin, HIGH);<br />
// Calculating the distance<br />
distance = duration * 0.034 / 2;<br />
// Prints the distance on the Serial Monitor<br />
Serial.print("Distance: ");<br />
Serial.println(distance);<br />
}<br />
</syntaxhighlight><br />
<br />
==Noise reduction code==<br />
<syntaxhighlight lang="c"> <br />
// Constants<br />
const int WINDOW_SIZE = 10; // Adjust the window size as needed<br />
const int sensorTriggerPin = 2; // Connect to the trigger pin of HC-SR04<br />
const int sensorEchoPin = 3; // Connect to the echo pin of HC-SR04<br />
const int glitchThreshold = 2000; // Threshold for excluding glitchy readings<br />
<br />
// Variables<br />
int readings[WINDOW_SIZE]; // Array to store recent sensor readings<br />
int index = 0; // Index for circular buffer<br />
long sum = 0; // Sum of recent readings<br />
<br />
void setup() {<br />
Serial.begin(9600);<br />
pinMode(sensorTriggerPin, OUTPUT);<br />
pinMode(sensorEchoPin, INPUT);<br />
}<br />
<br />
void loop() {<br />
// Trigger the sensor<br />
digitalWrite(sensorTriggerPin, LOW);<br />
delayMicroseconds(2);<br />
digitalWrite(sensorTriggerPin, HIGH);<br />
delayMicroseconds(10);<br />
digitalWrite(sensorTriggerPin, LOW);<br />
<br />
// Read the echo pulse duration<br />
long duration = pulseIn(sensorEchoPin, HIGH);<br />
<br />
// Calculate distance in centimeters<br />
float distance_cm = duration * 0.034 / 2;<br />
<br />
// Exclude glitchy readings<br />
if (distance_cm < glitchThreshold) {<br />
// Add the new reading to the window<br />
sum -= readings[index]; // Subtract the oldest reading<br />
readings[index] = distance_cm; // Store the new reading<br />
sum += distance_cm; // Add the new reading<br />
<br />
// Increment the index (circular buffer)<br />
index = (index + 1) % WINDOW_SIZE;<br />
<br />
// Calculate the moving average<br />
float movingAverage = static_cast<float>(sum) / WINDOW_SIZE;<br />
<br />
// Convert movingAverage to an integer<br />
int filteredDistance = static_cast<int>(movingAverage);<br />
<br />
// Print the filtered distance (as an integer)<br />
Serial.print("Filtered Distance: ");<br />
Serial.println(filteredDistance);<br />
<br />
// Add any additional logic or actions based on the filtered value<br />
// (e.g., control a motor, trigger an alarm, etc.)<br />
}<br />
<br />
// Add any other necessary logic here<br />
<br />
delay(100); // Adjust the delay as needed<br />
}<br />
<br />
<br />
<br />
</syntaxhighlight><br />
<br />
[[Category:Electronics]]</div>Annasahttps://interactionstation.wdka.hro.nl/wiki/TouchDesigner_%26_ArduinoTouchDesigner & Arduino2024-03-21T08:39:22Z<p>Annasa: /* Arduino wiring and code */</p>
<hr />
<div>You can use Arduino to send (sensor) data to TouchDesigner and vice versa. <br />
<br />
For example, you might want to measure the light condition of a room and use this data to trigger something in TouchDesigner. Or you might want to be able to have a light bulb react to a sound file, or a motor spin in sync with a projection.<br><br />
To do so there are typically to methods that can be used:<br />
*Firmata<br />
<br />
*Serial Communication<br />
With this method you are making Arduino & TD talk to each other by using Serial, a communication protocol that is able to send info essentially through a wire (the usb wire that connect the board to the computer running TD). TD has a '''Serial DAT''' operator that is able to receive this data. <br><br />
This method is preferred when your Arduino needs to use library or specific code to gather data from the sensors you are using. <br />
<br />
<br />
= Using Serial Communication=<br />
Serial communication is a method for transferring data between devices. It is able to send data sequentially over a wire. In this case the USB cable that connects your Arduino with the computer running TD.<br><br />
For this example we are getting sensor data from a LDR and a Ultrasonic sensor, sensing light values and distance. <br />
<br />
==Arduino to TouchDesigner==<br />
===Arduino wiring and code===<br />
[[File:ArduTD.jpg|thumb|400px]]<br />
<br />
First we wire the sensors to Arduino and upload the code to the board. The following example uses a [[Sensors:_Introduction_and_Types#LDR:_Light_Intensity_Sensor|LDR]] (light sensor) and an [[Sensors:_Introduction_and_Types#Ultrasonic_distance_sensor|Ultrasonic sensor]] (distance sensor). <br><br />
Here is the code: <br />
<syntaxhighlight lang="c"> <br />
//LDR PIN and VARIABLE<br />
int sensorPin = A0; // select the input pin for LDR<br />
int sensorValue = 0; // variable to store the value coming from the sensor<br />
//ULTRASONIC SENSOR PIN and VARIABLEs<br />
const int trigPin = 2;<br />
const int echoPin = 3;<br />
long duration;<br />
int distance;<br />
<br />
void setup() {<br />
//start serial communication<br />
Serial.begin(9600); //sets serial port for communication<br />
<br />
//define ultrasonic sensor pin as in or out <br />
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output<br />
pinMode(echoPin, INPUT); // Sets the echoPin as an Input<br />
}<br />
<br />
void loop() {<br />
//getting sensor data from LDR <br />
sensorValue = analogRead(sensorPin); // read the value from the sensor<br />
<br />
//getting sensor data from ultrasonic sensor <br />
// Clears the trigPin<br />
digitalWrite(trigPin, LOW);<br />
delayMicroseconds(2);<br />
// Sets the trigPin on HIGH state for 10 micro seconds<br />
digitalWrite(trigPin, HIGH);<br />
delayMicroseconds(10);<br />
digitalWrite(trigPin, LOW);<br />
// Reads the echoPin, returns the sound wave travel time in microseconds<br />
duration = pulseIn(echoPin, HIGH);<br />
// Calculating the distance<br />
distance = duration * 0.034 / 2;<br />
<br />
// Prints and sends the LDR and distance sensor values on the serial monitor on two separate lines <br />
Serial.print(sensorValue); <br />
Serial.print(" "); <br />
Serial.println(distance);<br />
}<br />
</syntaxhighlight><br />
<br />
We are initializing<br />
<br />
=TouchDesigner to Arduino=<br />
<br />
==Using Firmata==<br />
<br />
[[Category:TouchDesigner]]</div>Annasahttps://interactionstation.wdka.hro.nl/wiki/CPX-CircuitPythonCPX-CircuitPython2024-03-19T15:11:19Z<p>WangN: /* 7. CPX external inputs and outputs Wiring */</p>
<hr />
<div>=First things first: what is a microcontroller?=<br />
Computer and processor are generic terms for anything that can run a program, basically. <br><br />
A controller or microcontroller usually refers to a simple processor that does only one task, like listening to sensors. <br><br />
In explaining microcontrollers, we’ll distinguish them from computers, which contain more powerful processors that can run an operating system. <br><br />
One of the most used microcontroller is [[About Arduino | Arduino]]. In this case though we will be talking about Adafruit Circuit Playground, a board with a hand design for several project. <br />
<br />
=='''Adafruit Circuit Playground Express'''==<br />
<br />
How's that different from an [[About Arduino | Arduino]]?<br />
<br />
The Circuit Playground Express & Blue Fruit, produced by Adafruit, are microcontrollers, just like the Arduino Uno, but designed for wearable projects. <br />
<br />
Thus, is a little different from the normal boards from the hardware side, since the pins go out to those big wide sewable pads, instead of normal pin headers (like on the Uno) <br />
<br />
because Circuit Playground Express running on a faster processor compared to Arduino Uno. We can also use it for audio-based projects.<br><br />
[[File:Da1d-ADA-3333-1-0-2-1000x667.jpg|450px]]<br />
<br />
=='''CircuitPython & MU Editor'''==<br />
<br />
[[file:Circuitpython_circuit_playground_adafruit_blinka_computer.png|450px]] [[file:17850097.png|200px]]<br />
<br />
Python is the fastest-growing programming language. And CircuitPython is based on Python language but designed to run on microcontroller boards. <br />
<br />
CircuitPython is designed with education in mind. <br />
<br />
It's easy to start learning how to program and you get immediate feedback from the board. <br />
<br />
<br />
<br />
Before we start we all have to do this to set up:<br />
<br />
==='''1. Install or update CircuitPython!'''===<br />
<br />
https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart<br />
<br />
*1. Click the link below and download the latest UF2 file:<br />
https://circuitpython.org/board/circuitplayground_express/<br />
<br />
*2. Plug your Circuit Playground Express into your computer via a (data)USB cable.<br><br />
<br />
*3. Double-click the small Reset button in the middle of the CPX(short for circuit playground express).<br />
you will see all of the LEDs turn green. If they turn all red, check the USB cable, try another USB port, etc.<br />
(If double-clicking doesn't do it, try a single-click!)<br />
<br />
*4. You will see a new disk drive appear called '''CPLAYBOOT'''<br />
Drag the '''adafruit-circuitpython-etc...uf2''' file onto it.<br />
<br />
*5. The '''CPLAYBOOT''' drive will disappear and a new disk drive will appear called '''CIRCUITPY'''<br />
'''That's it! You're done :)'''<br />
<br />
==='''2. Installing Mu Edito'''r===<br />
<br />
Download Mu from(if you are using computers from school, the Mu Editor is already installed)<br />
<br />
https://codewith.mu<br />
<br />
[[File:Circuitpython mu-front-page.png|400px]]<br />
<br />
<br />
The first time you start Mu, you will be prompted to select your 'mode' - <br />
<br />
you can always change your mind later. For now please select '''CircuitPython!'''<br />
<br />
<br />
[[File:Circuitpython Screen Shot 2017-12-24 at 2.55.02 PM.png|400px]]<br />
<br />
<br />
Mu attempts to auto-detect your board, so please plug in your CircuitPython device and make sure it shows up as a CIRCUITPY drive before starting Mu<br />
<br />
[[File:Circuitpython Screen Shot 2017-12-24 at 3.16.23 PM.png]]<br />
<br />
<br />
=='''CPX onboard sensors & Pinouts'''==<br />
<br />
'''1. Green LED lets you know that the CPX is powered on.'''------ '''2. Red LED on Digital pin#13'''<br />
<br />
[[File:Circuit playground on-greenled.jpeg|400px]]<br />
[[File:Circuit playground d13.jpeg|400px]]<br />
<br />
'''3. This small button is for Resetting the board.''' ------------------ '''4. Button A and B on Digital pin #4 and #5'''<br />
<br />
[[File:Circuit playground reset.jpeg|400px]]<br />
[[File:Circuit playground pushbutton.jpeg|400px]]<br />
<br />
'''5. slide switch on Digital pin #7''' - - - - - - - - - - - - - - - - - - - - - - ''' 6. 7 Capacitive Touch pads from A1-A7'''<br />
<br />
[[File:Circuit playground slide.jpeg|400px]]<br />
[[File:Circuit playground captouch.jpeg|400px]]<br />
<br />
'''7. Light Sensor on pin A8''' - - - - - - - - - - - - - - - - - - - - - - - - - - - '''8. Temperature Sensor on A9'''<br />
<br />
[[File:Circuit playground lightsensor.jpeg|400px]]<br />
[[File:Circuit playground temp.jpeg|400px]]<br />
<br />
'''9. motion sensor (LIS3DH xyz) ''' - - - - - - - - - - - - - - - - - - - - - - - - - '''10. Microphone Audio Sensor'''<br />
<br />
[[File:Circuit playground adxl-motion.jpeg|400px]]<br />
[[File:Circuit playground mic.jpeg|400px]]<br />
<br />
'''11. speaker and audio output pin on A0'''<br />
<br />
[[File:Circuit playground spkr.jpeg|400px]]<br />
[[File:Circuit playground a0.jpeg|400px]] <br />
[[File:Circuit playground hp.png|400px]]<br />
<br />
'''12. Infrared Receive/Transmit''' <br />
<br />
[[File:Circuit playground ir.jpeg|400px]]<br />
<br />
<br />
'''Pinouts:'''<br />
<br />
----<br />
<br />
<br />
<br>'''A0 / D12''' - true analog output so it's great for playing audio clips.<br><br />
<br>'''A1 / D6''' - digital I/O, or analog Input, PWM output,capacitive touch sensor<br><br />
<br>'''A2 / D9''' - digital I/O, or analog Input, PWM output,capacitive touch sensor<br><br />
<br>'''A3 / D10''' - digital I/O, or analog Input.PWM output,capacitive touch sensor<br><br />
<br>'''A4 / D3''' - digital I/O, or analog Input, capacitive touch sensor<br><br />
<br>'''A5 / D2''' - digital I/O, or analog Input, capacitive touch sensor<br><br />
<br>'''A6 / D0''' - digital I/O, or analog Input, PWM output,capacitive touch sensor<br><br />
<br>'''A7 / D1''' - digital I/O, or analog Input, PWM output,capacitive touch sensor<be><br />
<br />
=='''Let's start coding'''==<br />
<br />
===3'''. Hello, World!'''===<br />
In the code area, where it says, # Write your code here :-), write the following code:<br />
<br />
'''print( 8 )'''<br />
<br />
Then click the Save button to save the code and run it on the CircuitPlayground.<br />
<br />
You should see this message appear in the Serial Dialogue Panel:<br />
<br />
'''code.py output:<br />
8'''<br />
<br />
Change the message to something else. If you want to print characters instead of numbers, you must use quotation marks.<br />
<br />
'''print( "hello" )'''<br />
<br />
Let's program the device to print two things, with a time delay in between the two print statements:<br />
<br />
'''import time'''<br />
<br />
'''print( "hello" )'''<br />
<br />
'''time.sleep( 0.5 )'''<br />
<br />
'''print( "World" )'''<br />
<br />
Now let's make a loop, so the message will be running forever:<br />
<br />
<syntaxhighlight lang="Python"><br />
#short cuts: Tab key , Shift-Tab, command(control)-k<br />
<br />
# Write your code here :-)<br />
<br />
import time<br />
<br />
while True:<br />
print("hello")<br />
time.sleep(0.5)<br />
print("world!")<br />
time.sleep(0.5)<br />
</syntaxhighlight><br />
<br />
<br />
'''Indentation and commenting options'''<br />
<br />
Python programs are Space holder Character Sensitive. <br />
<br />
To indent the three lines, I selected them all and then pressed the '''Tab key'''. <br />
<br />
To unindent, select some lines and press '''Shift-Tab.'''<br />
For commenting: select lines and press '''command(control)-k'''<br />
<br />
[[File:Mu CircuitPython Mode Cheat Sheet 1.jpg |600px]]<br />
<br />
==='''4. Interacting to the Serial Console''' ===<br />
<br />
<br />
==== '''The REPL'''====<br />
<br />
The other feature of the serial connection is the '''Read-Evaluate-Print-Loop''', or REPL. <br />
<br />
The REPL allows you to enter individual lines of code and have them run immediately. <br />
<br />
It's really handy if you're running into trouble with a particular program and can't figure out why.<br />
<br />
It's interactive so it's great for testing new ideas.<br />
<br />
To use the REPL, you first need to be connected to the serial console. <br />
<br />
Once that connection has been established, you'll want to press '''Ctrl + C'''.<br />
<br />
If there is code running, it will stop and you'll see Press any key to enter the REPL. Use '''CTRL-D to reload'''. <br />
<br />
Follow those instructions, and press any key on your keyboard.<br />
<br />
https://learn.adafruit.com/adafruit-circuit-playground-express/the-repl<br />
<br />
<br />
==='''6. CircuitPython Made Easy'''===<br />
<br />
====Libraries:====<br />
<br />
Libraries are codes other people have written for you. The<br />
libraries we will be using are included as part of the CircuitPython code, but you still<br />
must import them into your program. Other libraries, for many uses, must be<br />
downloaded and placed in '''a folder named lib''' on the CIRCUITPY drive. <br />
<br />
<br />
'''CircuitPython Libraries already included in CPX, But if you are using CPB,''' (CPX = Circuit Playground Express, CPB = Circuit Playground Bluefruit)<br />
<br />
'''you need to install CircuitPython Libraries by download the file and put them in the folder named lib on the CIRCUITPY drive.''' <br />
<br />
<br />
<br />
<br />
we will use circuit playground libraries, a simple way to include the Circuit Playground functionality in our code.<br />
<br />
more reference and examples:<br />
<br />
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express<br />
<br />
<br />
<br />
<br />
'''SAVE AS:'''<br />
<br />
if you want to save the code.py file in different names as well as in different locations:<br />
<br />
double click on the file name in Mu editor. <br />
<br />
[[File:Save-as-Capture.PNG|500px]]<br />
<br />
====Examples with CPX On-Board sensors:====<br />
<br />
====='''1. Red LED:'''=====<br />
<br />
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/red-led<br />
<br />
====='''2. Slide Switch:'''=====<br />
<br />
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/slide-switch<br />
<br />
<br />
====='''3. Neopixel'''=====<br />
<br />
======'''3.1 Smiley face'''======<br />
<syntaxhighlight lang="Python" ><br />
from adafruit_circuitplayground import cp<br />
cp.pixels.brightness=0.3<br />
<br />
while True:<br />
# the smiley's eyes<br />
cp.pixels[1]=(255,0,0)<br />
cp.pixels[3]=(255,0,0)<br />
#the smiley's mouth<br />
cp.pixels[5]=(255,0,0)<br />
cp.pixels[6]=(255,0,0)<br />
cp.pixels[7]=(255,0,0)<br />
cp.pixels[8]=(255,0,0)<br />
cp.pixels[9]=(255,0,0)<br />
<br />
</syntaxhighlight><br />
<br />
<br />
<br />
<syntaxhighlight lang="Python" ><br />
import time<br />
from adafruit_circuitplayground import cp<br />
cp.pixels.brightness=0.3<br />
<br />
while True:<br />
cp.pixels.brightness=0.3<br />
<br />
# the smiley's eyes<br />
cp.pixels[1]=(255,0,0)<br />
cp.pixels[3]=(255,0,0)<br />
#the smiley's mouth<br />
cp.pixels[5]=(255,0,0)<br />
cp.pixels[6]=(255,0,0)<br />
cp.pixels[7]=(255,0,0)<br />
cp.pixels[8]=(255,0,0)<br />
cp.pixels[9]=(255,0,0)<br />
time.sleep(3)<br />
cp.pixels.brightness=0<br />
<br />
#the smiley's eyes<br />
cp.pixels[1]=(255,255,0)<br />
cp.pixels[3]=(255,255,0)<br />
#the smiley's mouth<br />
cp.pixels[5]=(255,255,0)<br />
cp.pixels[6]=(255,255,0)<br />
cp.pixels[7]=(255,255,0)<br />
cp.pixels[8]=(255,255,0)<br />
cp.pixels[9]=(255,255,0)<br />
time.sleep(3)<br />
</syntaxhighlight><br />
<br />
======'''Neopixels for loops'''======<br />
<br />
<syntaxhighlight lang="Python" ><br />
import time<br />
from adafruit_circuitplayground import cp<br />
while True:<br />
for x in range(0,12):<br />
for j in range(0,10,1):<br />
print(j)<br />
cp.pixels[j]=(255,0,0)<br />
cp.pixels.brightness=0.3<br />
time.sleep(0.1)<br />
time.sleep(0.5)<br />
for j in range(9,-1,-1):<br />
print(j)<br />
cp.pixels.brightness=0.3<br />
cp.pixels[j]=(0,0,255) <br />
time.sleep(0.1)<br />
time.sleep(0.5)<br />
</syntaxhighlight><br />
<br />
====='''4. light sensor:'''=====<br />
<br />
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/light<br />
<br />
<syntaxhighlight lang="Python"><br />
import time<br />
import time<br />
from adafruit_circuitplayground import cp<br />
<br />
while True:<br />
print("Light:", cp.light)<br />
print(cp.light*5)<br />
#cp.play_tone(frequency, seconds)<br />
cp.play_tone(cp.light*5, 0.1)<br />
time.sleep(0.1)<br />
</syntaxhighlight><br />
<br />
====='''5. play tone:'''=====<br />
<br />
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-tone<br />
<br />
<br />
======'''Double Tap play tone and neopixel'''======<br />
<br />
<syntaxhighlight lang="Python"><br />
from adafruit_circuitplayground import cp<br />
# Change to 1 for detecting a single-tap!<br />
cp.detect_taps = 2<br />
cp.pixels.brightness=0.3<br />
<br />
while True:<br />
if cp.tapped:<br />
print("Tapped!")<br />
cp.red_led = True<br />
time.sleep(0.1)<br />
cp.play_tone(262, 1)<br />
for i in range(0,10,1):<br />
cp.pixels[i]=(255,0,0)<br />
time.sleep(0.5)<br />
<br />
else:<br />
cp.red_led = False<br />
for i in range(0,10,1):<br />
cp.pixels[i]=(0,0,255)<br />
</syntaxhighlight><br />
<br />
====='''capacitive touch piano:'''===== <br />
<br />
<syntaxhighlight lang="Python"><br />
<br />
import time<br />
from adafruit_circuitplayground import cp<br />
<br />
cp.pixels.brightness = 0.3<br />
<br />
while True:<br />
if cp.touch_A1:<br />
print("Touched A1!")<br />
cp.pixels.fill((255, 0, 0))<br />
cp.play_tone(262, 0.2)<br />
if cp.touch_A2:<br />
print("Touched A2!")<br />
cp.pixels.fill((210, 45, 0))<br />
cp.play_tone(294, 0.2)<br />
if cp.touch_A3:<br />
print("Touched A3!")<br />
cp.pixels.fill((155, 100, 0))<br />
cp.play_tone(330, 0.2)<br />
if cp.touch_A4:<br />
print("Touched A4!")<br />
cp.pixels.fill((0, 255, 0))<br />
cp.play_tone(349, 0.2)<br />
if cp.touch_A5:<br />
print("Touched A5!")<br />
cp.pixels.fill((0, 135, 125))<br />
cp.play_tone(392, 0.2)<br />
if cp.touch_A6:<br />
print("Touched A6!")<br />
cp.pixels.fill((0, 0, 255))<br />
cp.play_tone(440, 0.2)<br />
if cp.touch_TX:<br />
print("Touched TX!")<br />
cp.pixels.fill((100, 0, 155))<br />
cp.play_tone(494, 0.2)<br />
# time.sleep(0.1)<br />
<br />
</syntaxhighlight><br />
<br />
combine the code from examples "Play tone" and "light sensor". and use light sensor input to generate tones.<br />
<br />
====='''Conditionals:'''=====<br />
-----------------------------------------------------------------------------------------------------------------<br />
<br />
Evaluate a condition and react. If True, do this. If False, do this other thing.<br />
<br />
'''"while"''' creates a repeated action. while this condition is true, continue to do this thing over and over. <br />
<br />
when the condition is false, go on to the rest of the program.<br />
<br />
'''"if"''' creates a single action. if this condition is true, do this thing. if false, don't do it.<br />
<br />
'''"else"''' creates a single action, when the if the condition is false.<br />
<br />
'''for loop'''<br />
<br />
A for loop is used for iterating over a sequence. (in our previous example, we iterated through all the Neopixels on CPX using for loop)<br />
<br />
<br />
There is a CircuitPython reference PDF:<br />
<br />
[[File:CircuitPython Reference Sheet.pdf]]<br />
<br />
please read it after this lesson.<br />
<br />
====='''8. Play audio file'''=====<br />
<br />
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-file<br />
<syntaxhighlight lang="Python"><br />
<br />
#https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-file<br />
<br />
from adafruit_circuitplayground import cp<br />
<br />
cp.pixels.brightness = 0.3<br />
while True:<br />
if cp.button_a:<br />
cp.play_file("dip.wav")<br />
for x in range(5,10,1):<br />
cp.pixels[x]=(0,0,0)<br />
<br />
for y in range(0,5,1):<br />
cp.pixels[y]=(255,0,0)<br />
<br />
if cp.button_b:<br />
cp.play_file("rise.wav")<br />
for j in range(5,10,1):<br />
cp.pixels[j]=(0,255,50)<br />
<br />
for i in range(0,5,1):<br />
cp.pixels[i]=(0,0,0)<br />
<br />
</syntaxhighlight><br />
<br />
===7. CPX external inputs and outputs Wiring===<br />
<br />
for some external components that are not on board sensors.<br />
<br />
we need to install some library to make it work.<br />
<br />
that means we need to create a folder called "lib" on CIRCUITPY. <br />
<br />
and copy some files there. you can download the lib folder '''here:'''<br />
<br />
[[File:Lib.zip]]<br />
<br />
if you want to download and explore the full Bundle you can find they here:<br />
<br />
https://circuitpython.org/libraries<br />
<br />
<br />
[[File: Circuit playground Adafruit Circuit Playground Express Pinout.png|600px]]<br />
===='''Simple digital and analog inputs and output'''====<br />
<br />
<br />
'''1. Digital:'''<br />
<br />
https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out<br />
<br />
<br />
<br />
'''2. Digital in and out With external electronic components:'''<br />
<br />
'''After we followed the example, now let's try to connect an external Led.'''<br />
<br />
let's connect the led to A1(D6). There is a list of pinout from Adafruit's website:<br />
<br />
https://learn.adafruit.com/adafruit-circuit-playground-express/pinouts<br />
<br />
[[File:CircuitPlayground-express leds.jpg|500px]] <br />
<br />
'''instead of the onboard button, we can also use an external button'''<br />
<br />
[[File:CircuitPlayground-express leds-button.jpg|500px]]<br />
<br />
<br />
[[File:Digital-external.PNG|500px]]<br />
<br />
<br />
'''3. Analog input:'''<br />
<br />
https://learn.adafruit.com/circuitpython-essentials/circuitpython-analog-in<br />
<br />
<br />
'''4. Analog input and output with external electronic components:'''<br />
<br />
[[File:CircuitPlayground-express cpx-analog-read.jpg|500px]]<br />
<br />
[[File:Analog-read-led-code.PNG|500px]]<br />
<br />
import time<br />
import board<br />
import analogio<br />
<br />
from adafruit_circuitplayground import cp<br />
<br />
analogin = analogio.AnalogIn(board.A2)<br />
<br />
def getVoltage(pin):<br />
return (pin.value * 3.3) / 300<br />
<br />
while True:<br />
sensor = getVoltage(analogin)<br />
cp.play_tone(sensor,0.05)<br />
print("analog voltage:", sensor)<br />
time.sleep(0.01)<br />
<br />
===='''Other external wiring'''====<br />
<br />
'''Connect to external speakers:'''<br />
<br />
<br />
[[file:Circuit playground hp.png|600px]]<br />
<br />
<br />
<br />
'''Connect to servo motors:'''<br />
<br />
https://learn.adafruit.com/circuitpython-essentials/circuitpython-servo<br />
<br />
download lib here:[[File:Lib.zip]]<br />
<br />
single motor:<br />
<br />
[[file:Circuitpython safe.png|600px]]<br />
<br />
multiple motors:<br />
<br />
[[File:Circuit playground CProbot.png|600px]]<br />
<br />
<br />
you can find the library and code for the servo motors here:<br />
<br />
https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-servo<br />
<br />
<br />
ultra-sonic sensor:<br />
<br />
[[File:Sensors cpx-hcsr06-breadboard-crocclipse bb.png |600px]]<br />
<br />
https://learn.adafruit.com/jack-o-theremin/circuit-python-code<br />
[[Category:Circuit Playground Express]][[Category:Microcontrollers]] [[Category:Mu Editor]]<br />
<br />
===Fun projects made with CPX and CircuitPython Made Easy Lib===<br />
<br />
[[https://learn.adafruit.com/ufo-circuit-playground-express/code-the-ufo-with-circuitpython| UFO Flying Saucer]]<br />
<br />
[[https://learn.adafruit.com/combo-dial-safe-with-circuit-playground-express/overview?embeds=allow| Combo Dial Safe with Circuit Playground Express]]<br />
<br />
[[https://learn.adafruit.com/circuitpython-fruitbox-sequencer-musically-delicious-step-pattern-generator/code-with-circuitpython| FruitBox Sequencer: Musically Delicious Step Pattern Generator]]<br />
<br />
<br />
-Paged updated by NaN 2024<br />
[[Category:Microcontrollers]]</div>WangNhttps://interactionstation.wdka.hro.nl/wiki/Write_to_Arduino_No_firmataWrite to Arduino No firmata2024-02-29T15:50:49Z<p>WangN: </p>
<hr />
<div><br />
<br />
=1. Send data from Touchdesigner=<br />
== Firmata ==<br />
<br />
Palette —> tools —> Firmata<br />
<br />
== CHOP Execute DAT ===<br />
<br />
=2. Arduino received data=<br />
<br />
== Firmata ==<br />
file --> example --> Firmata --> StanderFirmata<br />
<br />
== Serial.read() ==<br />
===Arduino code ===<br />
<br />
<syntaxhighlight lang="C" line='line'><br />
#define led01 12<br />
#define led02 13<br />
//constant from Touchdesigner<br />
byte degreesTouchdesigner; //setpoint send by Touchdesigner<br />
<br />
void setup() {<br />
//start serial port at 9600 bps:<br />
Serial.begin(9600);<br />
while (!Serial)<br />
{<br />
;// wait for serial port to connect. Needed for native USB port only.<br />
} <br />
// Declare pins as output:<br />
pinMode(led01, OUTPUT);<br />
pinMode(led02, OUTPUT);<br />
}<br />
<br />
void loop() {<br />
if(Serial.available() > 0)<br />
{<br />
degreesTouchdesigner = Serial.read(); // read the values from Touchdesigner<br />
Serial.print(degreesTouchdesigner);<br />
if (degreesTouchdesigner>120)<br />
{<br />
digitalWrite(led01,LOW);<br />
digitalWrite(led02,HIGH); <br />
} <br />
else<br />
{<br />
digitalWrite(led02,LOW);<br />
digitalWrite(led01,HIGH);<br />
} <br />
}<br />
}<br />
</syntaxhighlight ><br />
<br />
===in TouchDesigner:===<br />
<br />
serial1 op<br />
<br />
'''one per byte'''<br />
<br />
CHOP Execute: '''select the CHOPS. Channels.'''<br />
<br />
code:<br />
<br />
<syntaxhighlight lang="python" line='line'><br />
# me - this DAT<br />
# <br />
# channel - the Channel object which has changed<br />
# sampleIndex - the index of the changed sample<br />
# val - the numeric value of the changed sample<br />
# prev - the previous sample value<br />
# <br />
# Make sure the corresponding toggle is enabled in the CHOP Execute DAT.<br />
<br />
def onOffToOn(channel, sampleIndex, val, prev):<br />
return<br />
<br />
def whileOn(channel, sampleIndex, val, prev):<br />
return<br />
<br />
def onOnToOff(channel, sampleIndex, val, prev):<br />
return<br />
<br />
def whileOff(channel, sampleIndex, val, prev):<br />
return<br />
<br />
def onValueChange(channel, sampleIndex, val, prev):<br />
op('serial1').sendBytes(val)<br />
return<br />
<br />
<br />
</syntaxhighlight><br />
<br />
[[Category:TouchDesigner]]</div>WangNhttps://interactionstation.wdka.hro.nl/wiki/Click_and_generate_a_random_numberClick and generate a random number2024-02-29T15:49:31Z<p>WangN: </p>
<hr />
<div>[[File:Noise01.png |600px]]<br />
[[File:Noise02.png |600px]]<br />
[[File:Noise03.png |600px]]<br />
<br />
[[Category:TouchDesigner]]</div>WangNhttps://interactionstation.wdka.hro.nl/wiki/TD-ExtrasTD-Extras2024-02-29T15:43:19Z<p>WangN: </p>
<hr />
<div> * [[click and generate a random number]]<br />
<br />
* [[Write to Arduino No firmata]]<br />
<br />
[[Category:TouchDesigner]]</div>WangNhttps://interactionstation.wdka.hro.nl/wiki/Visualizing_sound_in_TouchDesignerVisualizing sound in TouchDesigner2024-02-05T11:25:49Z<p>Annasa: </p>
<hr />
<div><br />
You can follow this tutorial to generate a dynamic sound wave in TouchDesigner starting from a sound file.<br><br />
Here an [https://interactionstation.wdka.hro.nl/mediawiki/images/d/d1/Smallest.mp4 example]. <br />
=Importing your sound file into TouchDesigner= <br />
<br />
* Open TouchDesigner<br />
* Remove the example network and get a blank view <br />
* Add your sound file: you can do this by directly dragging it inside the software view. It will automatically create a '''Audio File In''' named as the original file. Alternatively, you can add a '''Audio File In''' CHOP by double clicking on the pane view. You can then select your file from the operator menu. <br />
[[File:AW-1.png|600px|frameless]]<br />
* Add an '''Audio Device out''' and link it to your sound file. In this way you will be able to hear it. A file on its own will not automatically play without it. <br />
[[File:AW-2.png|300px|frameless]]<br />
* You might have noticed how the program is already giving you a visualisation of the audio file. Add a '''Audio Spectrum''' CHOP to display the frequency spectrum of the audio file. <br />
[[File:AW-3.png|600px|frameless]]<br />
* By default you will see the audio spectrum as a stereo view, visualising the two channels. You can turn this into one by adding a '''Math''' CHOP. <br>In the menu you should set: <br>'''Combine Channels : Average''' <br>This option will merge the two channels into one.<br />
[[File:AW-4.png|600px|frameless]]<br />
<br />
= Turning the CHOP view into an image=<br />
At this point you are seeing the audio spectrum of your file as a single graph. This image though is only the visualisation of a CHOP operator and not an image. CHOPS only understand channels of numerical data. We now need to turn this into an image - in this case a TOP. <br />
*From the exit point of your last Math block right click to see which operator can be link to it. We will select a '''CHOP to''' SOP. SOPs are are operators that can generate, import, modify and combine 3D geometries. We are using this to be able to translate the axis diagram into an image. <br>In the menu you should set: <br> '''Channel scope : chan1''' <br>(the channel name of your Math CHOP)<br> '''Attribute scope: P(1)'''<br>(This option will only select the Point Position on the Y Axis, the one we need to visualise)<br />
[[File:AW-5.png|600px|frameless]]<br />
* Connect the CHOP to SOP to a '''Geometry''' COMP. Do it by right clicking on the exit point of the last operator. <br />
[[File:AW-6.png|600px|frameless]]<br />
* Double click on the pane and add a '''Render''' TOP. You use this to render all 3D scenes. By default it will link to your geometry. At this point the graph is not visible. To make it visible we need to add a '''Camera''' and an '''Ambient Light''' COMP. They will both connect to the Render automatically. Now you should be able to see your sound wave image at the center of the Render TOP.<br> <br />
[[File:AW-7.png|600px|frameless]]<br> <br />
We can now do things like aligning the image to the bottom by using the '''Crop''' settings on the Render menu.<br> <br />
[[File:AW-8.png|600px|frameless]]<br />
= Add a Feedback Loop= <br />
At this point we managed to have our moving image out of the sound track. We are going to add a Feedback effect to it. <br />
* Add an '''Over''' TOP. The Over TOP is used to combine two inputs, adding the Input1 over the Input2. We are going to drag the render into the 2nd input of the Over TOP. <br />
[[File:AW-9.png|600px|frameless]]<br />
* Add a '''Transform''' TOP. Use the Render as an input and the over as an output. In the Transform menu change the '''Translate''' values to move the graph slightly up from its original spot. You can also change the unit from Fraction to Pixels.<br> You will now notice that another graph has been added on the Over operator. <br />
[[File:AW-10.png|600px|frameless]]<br />
* Add a '''Feedback''' TOP in between the Render and the Transform TOPs. You can do this by right clicking on the joint and select ''Insert Operator''.<br />
In the Feedback TOP menu place the Over operator under the Target TOP parameter. You can write here the name of the operator. <br />
You should now see how the Feedback operator is creating a loop that keeps adding layer at the distance determined in the Transform. You can of course have this loop with any other TOP. <br />
[[File:AW-11.png|600px|frameless]]<br />
You can now style your sound wave in the way you like using other TOPs. <br />
=Add a button to re-start the Loop=<br />
At this point your image will keep adding layers on the top of each other. But what if we want to clear out our image?<br><br />
In the Feedback menu you can find an option to '''Reset'''. By clicking on the '''Pulse''' button you can restart the loop. <br />
You can also trigger the Pulse from another parameter, like a ''Timer CHOP'' (to restart after a certain amount of time) or by pressing a certain key with a ''Keyboard In CHOP''. We are going to use the '''Button''' COMP. <br />
* Add a '''Button''' COMP. In the menu select '''Button Type: Momentary'''. This way the button state is changing only when pressed. Make the button active by clicking on the * symbol at the bottom right corner of the operator. Now you should be able to trigger it by pressing it.<br />
[[File:AW-12.png|600px|frameless]] <br />
* Add a '''Null''' CHOP and connect it to the button. You will see the value change when pressing the button. <br />
[[File:AW-13.png|600px|frameless]] <br />
* Make the Null CHOP active and "drag" it into the Reset Pulse option in the Feedback operator menu. Once the Null is active you will see it turning green when hovering it. Click on it and drag it in the Feedback parameter where you wish to export the value. An arrow symbol will appear when hovering the cell you wich to export your CHOP in. <br> Now you can restart the loop by pressing the button.<br />
[[File:AW-14.png|600px|frameless]] <br />
<br />
[[Category:TouchDesigner]]</div>Annasahttps://interactionstation.wdka.hro.nl/wiki/E-waste_binE-waste bin2024-01-17T10:07:45Z<p>Annasa: </p>
<hr />
<div>[[File:IMG 0238.jpg|400px]]<br><br />
At the Interaction Station we have a large E-waste bin. You can leave here old tech devices, electronic components, motors and appliances you are not using anymore. All these items are welcome, as they can be picked by someone else for their next project. You can also use these discarded items to get out some still valuable components.<br><br />
<span style="background-color:yellow;color:red;font-family:monospace">!!!When you take some components, please don't put things like plastic cases or non-electrical parts back in the bin!!!</span><br />
<br />
<br />
<span style="background-color:magenta;font-family:monospace">The E-Waste Bin is located in our Prototyping Space at WH.02.125. <br />
<br />
[[Category:Equipment]]<br />
[[Category:Electronics]]</div>Annasahttps://interactionstation.wdka.hro.nl/wiki/AxiDraw_V3/A3AxiDraw V3/A32024-01-17T08:45:32Z<p>Annasa: /* Create filled areas */</p>
<hr />
<div>[[File:1@2x.jpeg|600px]]<br><br />
A plotter is a tool that interprets commands from a computer to make line drawings on paper with pens, pencils and more. <br />
At the Interaction station we currently have 4 AxiDraw V3/A3 plotters. You can use them at the Station. <br />
<br />
== How to use it==<br />
You can check out this manual: [[File:AxiDraw V25b-compressed.pdf]].<br />
<br />
==How to prepare your files==<br />
===Create filled areas===<br />
https://wiki.evilmadscientist.com/Creating_filled_regions<br />
<br />
https://wiki.evilmadscientist.com/Hatch_fill<br />
<br />
[[Category:Plotters]]</div>Annasahttps://interactionstation.wdka.hro.nl/wiki/How_to_SolderHow to Solder2024-01-15T13:38:38Z<p>Annasa: </p>
<hr />
<div>[[File:Soldering pcb.jpeg|500px]]<br><br />
<span style="background-color:magenta;font-family:monospace;">The interaction Station is equipped with 5 soldering stations. They are located in our Prototyping space on WH.02.125.</span><br />
==Material needed==<br />
Accordingly to what you are going to solder you will need:<br />
*'''Soldering iron'''<br />
[[File:Soldering iron.png|300px]]<br><br />
They come with different tips. The fine one is used for precise jobs while the flat one makes the jobs faster. <br><br />
* '''Solder'''<br />
[[File:Solder.jpeg|300px]]<br><br />
Solder is a metal alloy material that is melted to create a permanent bond between electrical parts. It comes in bobbins and it can have different diameters. <br />
* '''Sponge'''<br />
[[File:Solder sponge.jpeg|250px]]<br><br />
Using a sponge will help to keep the soldering iron tip clean by removing the oxidation that forms. Clean the tip frequently while using the soldering iron. <br />
* '''Helping hand'''<br />
[[File:Screenshot 2024-01-15 at 14.32.14.png|300px]]<br><br />
A helping hand is a device that has 2 or more alligator clips and sometimes a magnifying glass/light attached. This clips will assist you by holding the items you are trying to solder while you use the soldering iron and solder. <br />
* '''Plyers and similar'''<br />
[[File:Plyers.png|300px]]<br />
* '''Wire stripper tool'''<br />
[[File:Wire stripper.jpeg|300px]][[File:Wire stripper1.jpeg|250px]]<br />
* '''Solder remover''' <br />
[[File:Solder remover.png|300px]]<br />
<div style="background-color:yellow;font-family:monospace;"><br />
!!!Warning!!!<br />
Some important tips before you start: <br />
*Air circulation<br />
Soldering produces fumes so proper air circulation is important. On the prototyping island (aka the table) there are wooden holes you can open to suck the soldering fumes. <br />
*Hot iron<br />
The soldering iron is really hot so use it carefully to avoid burnings. <br />
*Turn off the soldering iron if not in use<br />
Prolonged heating can damage and oxidate tip. As well, if you leave the iron on when not in use you run the risk of accidentally burning yourself or others. <br />
</div><br />
<br />
=Soldering instructions=<br />
<br />
*Prepare your work space!<br />
Make sure you have all you need around you to solder your components. <br />
<br />
*Turn on the soldering iron.<br />
Turn the know on the soldering station machine and select the temperature. For common soldering you shouldn't exceed the temperature of 300°. <br />
Higher temperature can oxidate the iron. <br />
<br />
==Solder wires==<br />
[[File:Solder wires.jpeg|500px]]<br><br />
'''1. Remove insulation'''<br />
<br />
To solder two wires together you first have to remove the plastic insulation layer that is wrapping the cable. You can use a wire stripper tool for this.<br />
<br />
2. Twist the wires together'''<br />
<br />
Twist the wire ends together. You can use a helping hand tool to keep those in place before soldering them. <br />
<br />
'''3. Heat the joint''' <br />
<br />
Place the soldering iron tip against the twisted wire joint. Heat both wires until they are hot enough to melt solder.<br />
'''<br />
4. Apply and distribute the solder'''<br />
<br />
The solder should melt and flow evenly around the wires, creating a solid connection.<br />
<br />
'''5. Let it cool'''<br />
<br />
Once you distributed the solder evenly remove the iron and tin. The joint will cool down in a few seconds.<br />
<br />
'''6. Make sure your joint is strong enough'''<br />
<br />
Pull the wire on both sides to make sure your joint is solid. <br />
<br />
'''7. Use heat shrink tube to secure and isolate the joint'''<br />
<br />
Insert a piece of heat shrink tube, place it on the joint and use a heat source to make the tube shrink around it. You can use a heat gun, a lighter or move it close to the soldering iron. Make sure you insert the tube before soldering if you will not have any end open after it.<br />
<br />
==Solder on circuit boards==<br />
[[File:Soldering.png|600px]]<br />
<br />
<br />
<br />
<br />
[[Category:Electronics]]</div>Annasa