Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save companje/b51839ec86dfe699a852a52fb3fa976a to your computer and use it in GitHub Desktop.
Save companje/b51839ec86dfe699a852a52fb3fa976a to your computer and use it in GitHub Desktop.
Control Excel 97 by Joystick and other sensors (work in progress).
#include <Keyboard.h>
const int pins[] = {2, 3, 4, 5, 6}; // Array van pinnummers
const int numButtons = sizeof(pins) / sizeof(pins[0]); // Aantal knoppen
const char teVersturenKarakters[numButtons] = {KEY_UP_ARROW, KEY_DOWN_ARROW, KEY_LEFT_ARROW, KEY_RIGHT_ARROW, ' '};
const unsigned long debounceTijd = 200; // Debounce tijd in milliseconden voor enkele druk
const unsigned long fastDebounceTijd = 100; // Verminderde debounce tijd voor snel opeenvolgende drukken
const unsigned long repeatStartTijd = 500; // Tijd in ms voordat herhaling begint
const unsigned long repeatInterval = 50; // Interval tussen herhalingen in ms
const unsigned long buttonReleaseThreshold = 100; // Drempel voor snelle drukdetectie
unsigned long lastPressTime[numButtons]; // Tijd van de laatste knopdruk voor elke knop
unsigned long lastReleaseTime[numButtons] = {0}; // Tijd van het laatste loslaten voor elke knop
unsigned long lastRepeatTime[numButtons]; // Tijd van de laatste herhaling voor elke knop
bool buttonPressed[numButtons]; // Status van elke knop
bool repeatStarted[numButtons]; // Herhalingsstatus voor elke knop
void setup() {
Keyboard.begin();
Serial.begin(9600); // Start de seriële communicatie
for (int i = 0; i < numButtons; i++) {
pinMode(pins[i], INPUT_PULLUP); // Stel elke knoppin in als input met pull-up
lastPressTime[i] = 0;
lastRepeatTime[i] = 0;
buttonPressed[i] = false;
repeatStarted[i] = false;
}
}
void loop() {
unsigned long currentTime = millis();
for (int i = 0; i < numButtons; i++) {
int buttonState = digitalRead(pins[i]);
unsigned long debounceDuration = (currentTime - lastReleaseTime[i] < buttonReleaseThreshold) ? fastDebounceTijd : debounceTijd;
if (buttonState == LOW) {
if (!buttonPressed[i] && (currentTime - lastPressTime[i] > debounceDuration)) {
//Serial.print(teVersturenKarakters[i]); // Stuur het karakter eenmaal
Keyboard.write(teVersturenKarakters[i]);
lastPressTime[i] = currentTime;
lastRepeatTime[i] = currentTime;
buttonPressed[i] = true;
repeatStarted[i] = false;
} else if (buttonPressed[i] && !repeatStarted[i] && (currentTime - lastPressTime[i] > repeatStartTijd)) {
repeatStarted[i] = true;
lastRepeatTime[i] = currentTime;
} else if (repeatStarted[i] && (currentTime - lastRepeatTime[i] > repeatInterval)) {
// Serial.print(teVersturenKarakters[i]); // Herhaal het karakter
Keyboard.write(teVersturenKarakters[i]);
lastRepeatTime[i] = currentTime;
}
} else {
if (buttonPressed[i]) {
lastReleaseTime[i] = currentTime;
}
buttonPressed[i] = false; // Reset de knop status als de knop losgelaten is
}
}
}
' first download and register mswinsck.ocx
' remove the kill-bit from the registry if needed
' create a userform. add the winsock ActiveX object to it.
'''''''''''''''''''''''''''''''''''
' put the following code in the ActiveX object code section by double clicking on the object
'''''''''''''''''''''''''''''''''''
Private Sub UserForm_Initialize()
With Winsock1
.LocalPort = 9999 ' De poort waar je naar berichten luistert
.Protocol = sckUDPProtocol ' Stel het protocol in op UDP
.Bind 9999 ' Bind de Winsock Control aan de poort
End With
'MsgBox ("activate")
End Sub
Function CharToHex(ch As String) As String
CharToHex = Hex(Asc(ch))
End Function
Function Strip(s As String)
Dim output As String
output = ""
Dim i As Integer
For i = 1 To Len(s)
If Mid(s, i, 1) = Chr(0) Then Exit For
output = output & Mid(s, i, 1)
Next i
Strip = output
End Function
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Winsock1.GetData s, vbString
s = Strip(s)
CyberAction (s)
End Sub
```
'''''''''''''''''''''''''''''''''''
' put the code below in a new 'Module'
'''''''''''''''''''''''''''''''''''
Public Sub CyberAction(s As String)
If s = "left" Then
If Not ActiveCell.Column = 1 Then
ActiveCell.Offset(0, -1).Select
End If
End If
If s = "right" Then
If Not ActiveCell.Column >= 100 Then
ActiveCell.Offset(0, 1).Select
End If
End If
If s = "up" Then
If Not ActiveCell.Row = 1 Then
ActiveCell.Offset(-1, 0).Select
End If
End If
If s = "down" Then
If Not ActiveCell.Row >= 100 Then
ActiveCell.Offset(1, 0).Select
End If
End If
If s = "zoom" Then
ActiveWindow.Zoom = 75
End If
If s = "fill" Then
If TypeOf Selection Is Range Then
'Selection.Interior.Color = RGB(255, 0, 0)
Selection.Interior.ColorIndex = 4
End If
End If
If s = "AddConditionalFormatting" Then
AddConditionalFormatting
End If
End Sub
Sub AddConditionalFormatting()
'Dim ws As Worksheet
'Set ws = ThisWorkbook.Sheets("Sheet1") ' Pas de naam "Sheet1" aan naar de naam van je werkblad
With Selection.FormatConditions
.Delete ' Verwijdert bestaande voorwaardelijke opmaakregels in het bereik
.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="100"
.Item(1).Interior.Color = RGB(255, 0, 0) ' Rood
End With
End Sub
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
void setup() {
size(150, 150);
oscP5 = new OscP5(this, 12345);
myRemoteLocation = new NetAddress("192.168.0.101", 9999);
}
void draw() {
}
void keyPressed() {
if (key==CODED) {
if (keyCode==LEFT) send("left");
if (keyCode==RIGHT) send("right");
if (keyCode==UP) send("up");
if (keyCode==DOWN) send("down");
} else {
if (key==' ') send("fill");
if (key=='z') send("zoom");
if (key=='f') send("AddConditionalFormatting");
}
background(random(255));
}
void send(String msg) {
println("send",msg);
OscMessage myMessage = new OscMessage(msg);
oscP5.send(myMessage, myRemoteLocation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment