#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
void
blink(){
digitalWrite(0,1);
delay(300);
digitalWrite(0,0);
}
char
get_key(){
int i;
int j;
for (j = 21; j < 25; ++j) {
digitalWrite(j, 1);
}
for (;;) {
for (i = 25; i < 29; ++i) {
if (digitalRead(i)) {
for (j = 21; j < 25; ++j) {
digitalWrite(j, 0);
if (!digitalRead(i)) {
blink();
switch (((i - 25) << 2) | (j - 21)) {
case 0: return '1';
case 1: return '4';
case 2: return '7';
case 3: return 'C';
case 4: return '2';
case 5: return '5';
case 6: return '8';
case 7: return '0';
case 8: return '3';
case 9: return '6';
case 10: return '9';
case 11: return '#';
case 12: return 'A';
case 13: return 'B';
case 14: return 'C';
case 15: return 'D';
}
}
}
}
}
}
}
int
get_num (
int count,
int x
){
char k;
printf("in get number");
while(count){
printf ("\ncount = %d", count);
k = get_key();
printf ("key = %c\n", k);
if (k == '#') return x;
if (('0' <= k) && (k <= '9')){
x = (x * 10) + (k - '0') ;
}
else return -1;
--count;
}
k = get_key();
if (k == '#') return x;
else return -1;
}
int
main()
{
int a;
int i,j;
int x,y,z;
int count;
wiringPiSetup();
pinMode(21, OUTPUT);
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, INPUT);
pinMode(26, INPUT);
pinMode(27, INPUT);
pinMode(28, INPUT);
pinMode(0, OUTPUT);
x = get_num( 6,0 );
printf("\nthe number = %d\n" , x );
}
ENGR 102 Fall 2015
Sunday, December 13, 2015
Wednesday, December 9, 2015
Sound Sensor code - 9 Dece 2015
#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>
#define PCF 120
int main (void)
{
int value;
int count = 0;
wiringPiSetup ();
// Setup pcf8591 on base pin 120, and address 0x48
pcf8591Setup (PCF, 0x48);
while(1) // loop forever
{
value = analogRead (PCF + 0);
//printf("%d\n", value);
if (value < 50){
count++;
printf("Voice In!! %d\n", count);
}
}
return 0;
}
#include <wiringPi.h>
#include <pcf8591.h>
#define PCF 120
int main (void)
{
int value;
int count = 0;
wiringPiSetup ();
// Setup pcf8591 on base pin 120, and address 0x48
pcf8591Setup (PCF, 0x48);
while(1) // loop forever
{
value = analogRead (PCF + 0);
//printf("%d\n", value);
if (value < 50){
count++;
printf("Voice In!! %d\n", count);
}
}
return 0;
}
Thursday, November 19, 2015
19 October 2015 - Button switch (momentary) example program and Pi 2B J8 Header
// Program button.c
//
#include <wiringPi.h>
#include <stdio.h>
// One side of the button is placed on GPIO 0
// The other side is on 5V
#define BtnPin 0
int
main(
){
wiringPiSetup();
pinMode(BtnPin,INPUT);
for(;;)
{
if(0==digitalRead(BtnPin))
{
printf("\nButton is NOT Pressed\n");
}
if(1==digitalRead(BtnPin))
{
printf("\nButton is Pressed\n");
}
}
return(0);
}
//
#include <wiringPi.h>
#include <stdio.h>
// One side of the button is placed on GPIO 0
// The other side is on 5V
#define BtnPin 0
int
main(
){
wiringPiSetup();
pinMode(BtnPin,INPUT);
for(;;)
{
if(0==digitalRead(BtnPin))
{
printf("\nButton is NOT Pressed\n");
}
if(1==digitalRead(BtnPin))
{
printf("\nButton is Pressed\n");
}
}
return(0);
}
Tuesday, November 10, 2015
DC Motor Control using PWM for Speed Control
In this exercise, we use DC Motor control using PWM for speed control. The electronic driver is called an H-Bridge that will allow the motor to turn either way if hooked up properly. That will be shown in a follow-on Blog entry.
We us a 31162MP H-Driver as hooked up to the Raspberry Pi 2B as shown:
https://www.dropbox.com/s/c0xvxc32x4q75tn/hDrivePi.pptx?dl=0
The specification for the 31162MP H-Driver board is:
https://www.dropbox.com/s/m2cym0tm2e7c2f4/31162mp-Hdriver.pdf?dl=0
Test code using a single speed is shown below. The speed can be set depending on the PWM pulse width of 0-1023. Below a value of about 150, the motor may not turn.
https://www.dropbox.com/s/p5e6wjhjwrra2s2/pwmtest1.c?dl=0
We us a 31162MP H-Driver as hooked up to the Raspberry Pi 2B as shown:
https://www.dropbox.com/s/c0xvxc32x4q75tn/hDrivePi.pptx?dl=0
The specification for the 31162MP H-Driver board is:
https://www.dropbox.com/s/m2cym0tm2e7c2f4/31162mp-Hdriver.pdf?dl=0
Test code using a single speed is shown below. The speed can be set depending on the PWM pulse width of 0-1023. Below a value of about 150, the motor may not turn.
https://www.dropbox.com/s/p5e6wjhjwrra2s2/pwmtest1.c?dl=0
Continuous Temperature Sensor with LED Indicator Program
Continuous Temperature Sensor with LED Indicator Program using linked compile for the functions.
The main program: tempmain.c
GPIO Setup program (function): setupPi.c
LED Off program (function): leds_off.c
Read Temperature Sensor program (function): readTemp.c
Turn on LED Corresponding to Temperature (I used 3 temperature ranges): led_temp_on.c
To compile:
gcc tempmain.c setupPi.c leds_off.c readtemp.c led_temp_on.c -lwiringPi
Single Pass Example Temperature Sensor with LED Indicator Program
SINGLE PASS Example Temperature Sensor with LED Indicator Program using linked compile for the functions.
The main program: tempmainEx.c
GPIO Setup program (function): setupPiEx.c
LED Off program (function): leds_offEx.c
Read Temperature Sensor program (function): readTempEx.c
Turn on LED Corresponding to Temperature (I used 3 temperature ranges): led_temp_onEx.c
To compile:
gcc tempmainEx.c setupPiEx.c leds_offEx.c readtempEx.c led_temp_onEx.c -lwiringPi
You'll see on the display when certain entries and exits to and from functions occur.
Subscribe to:
Posts (Atom)
