-
Notifications
You must be signed in to change notification settings - Fork 1
/
program.c
61 lines (46 loc) · 1.19 KB
/
program.c
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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "softPi.h"
#define SWITCH 14
#define MOTOR_1 15 // RIGHT
#define MOTOR_DIR_1 16
#define MOTOR_2 17 // LEFT
#define MOTOR_DIR_2 18
void alert(int gpio, int level, uint32_t tick)
{
if (PI_ON == level)
{
printf("Bump!\n");
// Backwards one second
gpioWrite(MOTOR_DIR_1, PI_ON);
gpioWrite(MOTOR_DIR_2, PI_ON);
sleep(1);
// Forwards again, but slower on one side for a second
gpioWrite(MOTOR_DIR_1, PI_OFF);
gpioWrite(MOTOR_DIR_2, PI_OFF);
gpioPWM(MOTOR_1, 50);
gpioPWM(MOTOR_2, 60);
sleep(1);
// Normal again
gpioPWM(MOTOR_1, 255);
gpioPWM(MOTOR_2, 255);
}
}
int main(int argc, char *argv[])
{
int secs=600;
if (gpioInitialise()<0) return 1;
gpioSetMode(SWITCH, PI_INPUT);
gpioSetMode(MOTOR_DIR_1, PI_OUTPUT);
gpioSetMode(MOTOR_DIR_2, PI_OUTPUT);
gpioWrite(MOTOR_DIR_1, PI_OFF);
gpioWrite(MOTOR_DIR_2, PI_OFF);
gpioPWM(MOTOR_1, 255);
gpioPWM(MOTOR_2, 255);
gpioSetAlertFunc(SWITCH, alert);
sleep(secs);
gpioTerminate();
}