forked from LukasWestholt/Calliope-Mini-Callicar-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_Project-Callicar_FernbedienungV-4.js
115 lines (106 loc) · 3.04 KB
/
_Project-Callicar_FernbedienungV-4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
let y_axis = 0
let x_axis = 0
let alarm = false
let show_led = [-1, -1]
// Um Gyrosteuerung zu deaktivieren und Knopfsteuerung als Startmodus zu wählen - setze diesen Wert zu 1
// To deactivate gyro-control and to set the startmode to button-control - set this to 1
let play_mode = 0 // Default: 0
// Wenn das Auto im Modus Knopfsteuerung ist, wie sehr soll dann zu Seite gelenkt werden? Standard: 800
const DIRECTION_BUTTON_CONTROL = 800 // Default: 800
oledssd1306.initDisplay()
oledssd1306.setTextXY(0, 2)
oledssd1306.writeString("Geschwindigkeit:")
// Einstellungen für die Drahtlosverbindung
radio.setGroup(0)
radio.setTransmitPower(7) // Max=7
radio.onDataPacketReceived(({receivedString: m, receivedNumber: d}) => {
let dataInt = d
let mode = m
if (dataInt != dataInt) {
// dataInt is NaN
dataInt = 0
}
if (mode == "retunspeed") {
let speeddata = dataInt
}
})
// Buttons
basic.forever(() => {
if (play_mode == 0) {
if (input.buttonIsPressed(Button.A)) {
alarm = true
radio.sendValue("alarm", 1)
music.playTone(262, music.beat(BeatFraction.Quarter))
basic.setLedColor(Colors.Purple)
} else if (alarm) {
alarm = false
basic.setLedColor(0)
}
if (input.buttonIsPressed(Button.B)) {
// wechsele zu Knopfsteuerung
play_mode = 1
basic.pause(300) // Diese Pause ist notwendig, weil sonst der B-Knopf direkt als Steuerung erkannt wird
}
} else if (play_mode == 1) {
// manuelles Fahren
if (input.buttonIsPressed(Button.A) && input.buttonIsPressed(Button.B)) {
x_axis = 0
y_axis = -1023
} else if (input.buttonIsPressed(Button.A)) {
x_axis = -1 * DIRECTION_BUTTON_CONTROL
y_axis = -1023
} else if (input.buttonIsPressed(Button.B)) {
x_axis = DIRECTION_BUTTON_CONTROL
y_axis = -1023
} else {
x_axis = 0
y_axis = 1023
}
basic.setLedColor(Colors.Yellow)
}
})
// LED-Matrix & Fernsteuerung
basic.forever(() => {
if (play_mode == 0) {
// Sensordaten
x_axis = input.acceleration(Dimension.X)
y_axis = input.acceleration(Dimension.Y)
}
// Setze neue LED-Koordinate
led.unplot(show_led[0], show_led[1])
show_led[0] = pins.map(
x_axis,
-1023,
1023,
-1,
5
)
if (show_led[0] == -1) {
show_led[0] = 0
}
if (show_led[0] == 5) {
show_led[0] = 4
}
show_led[1] = pins.map(
y_axis,
1023,
-1023,
5,
-1
)
if (show_led[1] == -1) {
show_led[1] = 0
}
if (show_led[1] == 5) {
show_led[1] = 4
}
led.plot(show_led[0], show_led[1])
// Sende Daten an Auto
radio.sendValue("direction", x_axis)
radio.sendValue("speed", y_axis)
})
basic.forever(function() {
oledssd1306.setTextXY(1, 7)
oledssd1306.writeNumber(speeddata)
basic.pause(1000)
})