currently doing a project with a board with several routines:
#include <xc.h>
#include "config.h"
#define TRUE 1
#define FALSE 0
#define SW1 PORTBbits.RB4
#define SW2 PORTBbits.RB5
#define PB3 PORTCbits.RC3 //for testing
#define PB2 PORTDbits.RD1
#define PB7 PORTCbits.RC7
#define PB8 PORTCbits.RC6
#define MOTOR PORTDbits.RD0
void initSysPins(void);
void ReadLight(void);
void ReadTemperature(void);
void ReadSoilMoisture(void);
void MessageDoze(void);
void MessageSleep(void);
void lcdClear(void);
void initLCD(void);
void initSysInt(void);
void initSysTimer(void);
void pwrMgmt(void);
void initPWM(void);
unsigned char detPB3(void); //for testing
unsigned char detPB2(void);
unsigned char detPB7(void);
unsigned char detPB8(void);
void main(void) {
initSysInt();
initSysPins();
initSysTimer();
CPUDOZE = 0;
while(TRUE){
if(SW1 == 1 && SW2 == 0){
ReadTemperature();
}
else if (SW1 == 0 && SW2 == 1){
ReadLight();
}
else if (SW1 == 1 && SW2 == 1){
ReadSoilMoisture();
}
else {
lcdClear();
}
pwrMgmt();
//pwrMgmt1();
}
}
void pwrMgmt(void){
if(PB2 == 0){
lcdClear();
ROI = 1;
MessageSleep();
SLEEP();
__delay_ms(500);
}
else if(PB7 == 0){
lcdClear();
MessageDoze();
DOZEN= 1;
ROI = 1;
}
}
unsigned char detPB3(void)
{
unsigned char detect = 0; //initial status, sw1 not pressed
if(PB3 == 0) //if sw1 is pressed
{
__delay_ms(20); // 20ms delay to skip contact bounce
if(PB3 == 0) //if still pressed
{
detect = 1; //confirm sw1 is pressed n set detect
while(PB3 == 0);//wait for sw1 to be released
}
}
return detect;
}
unsigned char detPB2(void)
{
unsigned char detect = 0; //initial status, sw1 not pressed
if(PB2 == 0) //if sw1 is pressed
{
__delay_ms(20); // 20ms delay to skip contact bounce
if(PB2 == 0) //if still pressed
{
detect = 1; //confirm sw1 is pressed n set detect
while(PB2 == 0);//wait for sw1 to be released
}
}
return detect;
}
unsigned char detPB7(void)
{
unsigned char detect = 0; //initial status, sw1 not pressed
if(PB7 == 0) //if sw1 is pressed
{
__delay_ms(20); // 20ms delay to skip contact bounce
if(PB7 == 0) //if still pressed
{
detect = 1; //confirm sw1 is pressed n set detect
while(PB7 == 0);//wait for sw1 to be released
}
}
return detect;
}
unsigned char detPB8(void)
{
unsigned char detect = 0; //initial status, sw1 not pressed
if(PB8 == 0) //if sw1 is pressed
{
__delay_ms(20); // 20ms delay to skip contact bounce
if(PB8 == 0) //if still pressed
{
detect = 1; //confirm sw1 is pressed n set detect
while(PB8 == 0);//wait for sw1 to be released
}
}
return detect;
}
The only problem is that my DC motor still runs on its own despite only being called inside my soil moisture function. the other functions just use LCD and LED and work fine. I've tried working around this by putting MOTOR = 0; inside my main but that doesn't do anything.
EDIT: Works on its own before integration with the rest of my codes.
ReadSoilMoisture():
#include <xc.h>
#include "config.h"
#define lcd_Clear 0b00000001
#define LCD_DATA PORTD
#define LCD_RS PORTEbits.RE1 //RS signal for LCD
#define LCD_E PORTEbits.RE0 // E signal for LCD
#define MOTOR PORTDbits.RD0
void initSysPins(void);
void lcdWriteData(char);
void lcdWriteCtrl(char);
void lcdWriteMessage(char message[], unsigned char row);
void lcdSetPos(unsigned char row, unsigned char col);
void initPWM(void);
//void Message(void);
void Message1(void);
void Message2(void);
void Message(void);
void initLCD(void);
void initADC(void);
unsigned int readPot1(void);
void ReadSoilMoisture(void) {
unsigned int raw;
unsigned char pwmCycle = 0;
// float fresult;
// char res[20];
initLCD();
initSysPins();
initPWM();
initADC();
PWM6CONbits.EN = 1;
// raw = readPot1();
int i = 0;
for(i = 0; i<=40; i++) {
raw = readPot1();
if (raw == 0 && raw <= 10)
{
PORTA = 0x00;
}
else if (raw == 11 && raw <=100)
{
PORTA = 0b00001001; //RED
MOTOR = 1;
pwmCycle = 4000;
__delay_ms(5000);
PWM6DCH = pwmCycle >> 2;
PWM6DCL = (pwmCycle % 4)<< 6;
Message();
__delay_ms(3000);
lcdWriteCtrl(0b00000001); // Clear display & home position
}
else if (raw == 101 && raw <= 1000)
{
PORTA = 0b00010010; //YELLOW
MOTOR = 1;
pwmCycle = 1500;
__delay_ms(3000);
PWM6DCH = pwmCycle >> 2;
PWM6DCL = (pwmCycle % 4)<< 6;
Message1();
__delay_ms(3000);
lcdWriteCtrl(0b00000001); // Clear display & home position
}
else if (raw == 1001 && raw <= 1023)
{
PORTA = 0b00100100; //GREEN
MOTOR = 1;
pwmCycle = 900;
__delay_ms(800);
PWM6DCH = pwmCycle >> 2;
PWM6DCL = (pwmCycle % 4)<< 6;
Message2();
__delay_ms(3000);
lcdWriteCtrl(0b00000001); // Clear display & home position
}
}
}
void Message(void){
char message[] = "Soil Moisture";
char message1[] = "Is 55%";
unsigned char i;
lcdSetPos(1,1); // Set DD RAM pos to row 1, col 3
for(i = 0; message[i] != 0; i++) {
lcdWriteData(message[i]); // Display one character
}
lcdSetPos(2,1); // Set DD RAM pos to row 2, col 1
for(i = 0; message1[i] != 0; i++) {
lcdWriteData(message1[i]); // Display one character
}
}
void Message1(void){
char message[] = "Soil Moisture";
char message1[] = "Is 75%";
unsigned char i;
lcdSetPos(1,1); // Set DD RAM pos to row 1, col 3
for(i = 0; message[i] != 0; i++) {
lcdWriteData(message[i]); // Display one character
}
lcdSetPos(2,1); // Set DD RAM pos to row 2, col 1
for(i = 0; message1[i] != 0; i++) {
lcdWriteData(message1[i]); // Display one character
}
}
void Message2(void){
char message[] = "Soil Moisture";
char message1[] = "Is 95%";
unsigned char i;
lcdSetPos(1,1); // Set DD RAM pos to row 1, col 3
for(i = 0; message[i] != 0; i++) {
lcdWriteData(message[i]); // Display one character
}
lcdSetPos(2,1); // Set DD RAM pos to row 2, col 1
for(i = 0; message1[i] != 0; i++) {
lcdWriteData(message1[i]); // Display one character
}
}
void initPWM(void)
{
CCPTMRS1 = PORTDbits.RD0;
PWM6CON = 0b00000000;
T2PR = 249;
PWM6DCH = 0b01111101;
PWM6DCL = 0b00000000;
T2CON = 0b11110000;
T2CLKCON = 0b00000001;
RA2PPS = 0x0E;
}
Any guess as to what I can do?
void UART_init(void){
ANSELB = 0; //set PORT B to digital port
TRISBbits.TRISB5 = 1; //set RX pin to input
TRISBbits.TRISB7 = 0; //set TX pin as output
SPBRGH = 0;
SPBRGL = 25; //set baud rate to 9600
BRGH = 0;
BRG16 = 0;
SYNC = 0;
SPEN = 1; //enable serial port pins
TX9 = 0; //set 9 bit tranmission
RX9 = 0; //set 9 bit receive
TXEN = 1; //enable transmission
CREN = 1; //enable receiver
}
void UART_write(char data){
while(TRMT == 0);
TXREG = data;
}
void UART_write_string(char *text){
for(int i=0; text[i] != '\0'; i++){
UART_write(text[i]);
}
}
char UART_read(){
while(RCIF == 0);
return RCREG;
}
char URAT_read_string(char *stringprimit, int lungime){
for(int i=0; i < lungime; i++){
stringprimit[i] = UART_read();
}
}
int main(int argc, char** argv) {
OSCCONbits.IRCF = 0b1111; //set operating frequency to 31kHz (0b1111) for 16MHz
//WDTCONbits.WDTPS = 0b01110;
//CLRWDT();
//0b01100; //set WTD interval at 4s
UART_init();
//Activam pull-up
OPTION_REGbits.nWPUEN = 0;
WPUCbits.WPUC2 = 1;
WPUCbits.WPUC6 = 1;
WPUCbits.WPUC7 = 1;
WPUCbits.WPUC0 = 0;
WPUCbits.WPUC1 = 0;
WPUCbits.WPUC3 = 1;
//Led-uri
TRISAbits.TRISA1 = 0; // set as output
TRISAbits.TRISA2 = 0; // set as output
ANSELAbits.ANSA1 = 0; //pin digital
ANSELAbits.ANSA2 = 0; //pin digital
ANSELAbits.ANSA0 = 1; //set to analogic pin
TRISAbits.TRISA0 = 1; //set as input
ANSELCbits.ANSC0 = 0; //set to digital pin
ANSELCbits.ANSC1 = 0; //set to digital pin
TRISCbits.TRISC0 = 0; //set as output
TRISCbits.TRISC1 = 0; //set as output
ANSELCbits.ANSC2 = 0; //set to digital pin
ANSELCbits.ANSC6 = 0; //set to digital pin
ANSELCbits.ANSC7 = 0; //set to digital pin
TRISCbits.TRISC2 = 1; //set as input
TRISCbits.TRISC6 = 1; //set as input
TRISCbits.TRISC7 = 1; //set as input
PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;
char user_input;
UART_write_string("1. rotate right ");
UART_write('\r');
UART_write_string("2. rotate left");
UART_write('\r');
UART_write_string("3. stop");
UART_write('\r');
UART_write_string("4. Deactivate UART");
UART_write('\r');
UART_write_string("select:");
while(1){
//CLRWDT();
user_input = UART_read();
if(PORTCbits.RC7 == 0 ){ //motor rotates right
user_input = '1';
PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;
}
if(PORTCbits.RC6 == 0 ){ //motor rotates left
user_input = '2';
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 1;
PORTAbits.RA1 = 1;
PORTAbits.RA2 = 0;
}
if(PORTCbits.RC2 == 0 ){ //motor stop
user_input = '3';
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 0;
}
if(user_input =='1') {//motor rotates right
PORTCbits.RC0 = 1;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 1;
}
if( user_input =='2'){ //motor rotates left
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 1;
PORTAbits.RA1 = 1;
PORTAbits.RA2 = 0;
}
if(user_input =='3'){ //motor stop
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 0;
}
if(user_input =='4'){ //deactivate UART
CREN = 0;
}
}
return (EXIT_SUCCESS);
}
In this project I tried to control a DC motor with two methods, the first method is UART with user's input and the second with buttons.
The scope: When I press the numer 1 from the keyboard I want the motor rotates to the right, 2- left, 3 - stop. This works properly, but the problem is with the buttons, when I press the RC7 button nothing happens.
RC7 - means the motor rotates right, RC6 - them motor rotates left, RC2 - the motor stop
From what I can tell, my program doesn't even see those conditions, like PORTbits.RC7 == 0.
From the code, I can see that the function:
char UART_read(){
while(RCIF == 0);
return RCREG;
}
is blocking on while(RCIF == 0);
and I think this is the reason your Buttons are not working because at the very beginning of the while(1) you have called UART_read();.
And you mentioned it yourself that the output is as expected for UART data, so this answers your question.
To make it work, you have to write a UART Rx Interrupt so that your program should not be blocking/waiting on UART to Receive data.
Edit:
As per your comment, that you are disabling the UART reception using CREN, then you should also check if the Receiver is enabled prior to reading the Rx Data register, you can modify the UART receive function as below:
char UART_read(){
if(CREN == 0) {
return '\0'; //Return NULL Character
}
while(RCIF == 0);
return RCREG;
}
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int m = 1;
int brightness = 10;
int fadeAmount = 5;
int led;
int one = 1;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop()
{
if (m == 4 or m == 1) {
one = -one;
}
if (intensite <= 5){
m = m + one;
}
if (m == 1) {
led = led1;
}
if (m == 2) {
led = led2;
}
if (m == 3) {
led = led3;
}
if (m == 4) {
led = led4;
}
brightness = brightness + fadeAmount;
if (brightness <= 5 or brightness >= 255) {
fadeAmount = -fadeAmount;
}
analogWrite(led,brightness);
delay(10);
}
When I run this part of the code, it just gets stuck in a infinite loop at the top led and it does not go back down the way it should which I fail to see. It would be of great help if one could help me understand my mistake. The aim is to make a code that lights leds from right to left (led1 to led4) then left to right (led4 to led1), not simply turning them on but fading them.
I tried my best to stay as close to your code as possible. So your code didn't work because it don't make much sense. Why would you make this one variable? It just complicated all the things. Just put a boolean which indicates whether you are going left or not and change it based on the current value of m. Here is how:
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int m = 1;
int brightness = 10;
int fadeAmount = 5;
int led;
bool goingLeft;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop()
{
if (m == 1) { // If we are on the right side we set goingLeft to true
goingLeft = true;
}else if(m == 4){ // If we are on the left side we set goingLeft to false
goingLeft = false;
}
if (brightness <= 5 && goingLeft){ // if we are moving left then we increment the m variable
m++;
}else if(brightness <= 5){ // if we are moving right we decrement it
m--;
}
if (m == 1) {
led = led1;
}
if (m == 2) {
led = led2;
}
if (m == 3) {
led = led3;
}
if (m == 4) {
led = led4;
}
brightness = brightness + fadeAmount;
if (brightness <= 5 or brightness >= 255) {
fadeAmount = -fadeAmount;
}
analogWrite(led,brightness);
delay(10);
}
If I were you I would do this in a completly diffrent way by using for loops but as I said earlier I tried to stay as close to your code as I could.
Also I saw that you used or in your code. Don't do it!!! In C languages the or operator is || not or
I am trying to read an RFID tag and compare the RFID number with a array which I have already there. In my code, the comparing is not working properly. It always gives output as both 'Found' and 'Not found'. Can any one help me on this?
The two RFID numbers that I want to read are: 37376B34 and 7AA29B1A. These two are only for testing. I'm going to store about 20 RFID numbers in the array and check.
My code:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int readsuccess;
byte readcard[4];
char str[32] = "";
String StrUID;
char* myTags[] = {"9FF4375C","37376B34","7AA29B1A","1B7D5223","9FF4375C"};
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
// --------------------------------------------------------------------
void loop() {
readsuccess = getid();
if (readsuccess) {
Serial.println(StrUID);
delay(1000);
}
for (int i = 0; i < 5; i++) {
if (StrUID == myTags[i]) {
Serial.println("Found");
return;
} else {
Serial.println("Not Found");
}
}
}
// --------------------------------------------------------------------
int getid() {
if (!mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return 0;
}
for (int i=0; i<4; i++) {
readcard[i] = mfrc522.uid.uidByte[i]; //storing the UID of the tag in readcard
array_to_string(readcard, 4, str);
StrUID = str;
}
mfrc522.PICC_HaltA();
return 1;
}
// --------------------------------------------------------------------
void array_to_string(byte array[], unsigned int len, char buffer[]) {
for (unsigned int i = 0; i < len; i++) {
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len * 2] = '\0';
}
Data type byte in Arduino is just unsigned char in c++. Since the data that you read from the reader are in bytes, so it is possible to directly compare it with myTags without the need of using String object or even the array to string conversion. You can do the comparison with C++ memcmp() function.
const char* myTags[] = {"9FF4375C","37376B34","7AA29B1A","1B7D5223","9FF4375C"};
byte myReading[4] = {0x9f,0xf4,0x37,0x5c};
for (int i = 0; i < 5; i++) {
if (memcmp(myTags[i], myReading, sizeof(myReading)) == 0) {
Serial.println("Found");
return;
} else {
Serial.println("Not Found");
}
}
I'm making one-buttoned bandit slot machine made on 3x 7 segment displays and 3x 74HC595 Shift Registers. Problem is that I want to have some kind of indicator that you won (or you partially won - 2 numbers are the same).
losLiczb(latchPin3, dataPin3, clockPin3, data3, liczba3); // liczba3 = in eg. 4
Serial.println("Wylosowana liczba to"); // "Generated number:"
Serial.println(liczba3); // liczba3 = 0
So, liczba1/liczba2/liczba3 is number which was generated in function losLiczb. Main problem is that liczba1/2/3 have assigned number inside function losLiczb (I can chceck correctness via Serial Read), but outside the number liczba1/2/3 is 0, thus program will always indicate that you won. Here is the whole program if you want (some of comments are in polish and are unnessecary,:
int latchPin1 = 5; //Pin connected to ST_CP of 74HC595
int clockPin1 = 4; //Pin connected to SH_CP of 74HC595
int dataPin1 = 6; //Pin connected to DS of 74HC595
int latchPin2 = 8;
int clockPin2 = 7;
int dataPin2 = 9;
int latchPin3 = 11; //11
int clockPin3 = 12; //12
int dataPin3 = 10; //8
int liczba1;
int liczba2;
int liczba3;
const int buttonPin = 2;
int statusPin = 3;
int buttonState = 0;
//holders for infromation you're going to pass to shifting function
byte data;
byte data1;
byte data2;
byte data3;
byte dataArray[10];
byte animacja[8];
byte dummy[1];
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin1, OUTPUT);
pinMode(latchPin2, OUTPUT);
pinMode(latchPin3, OUTPUT);
pinMode(statusPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
//Arduino doesn't seem to have a way to write binary straight into the code
//so these values are in HEX. Decimal would have been fine, too.
// 00000000 - trzeba zapisywać w translatorze hex-bin w takiej postaci.
// Kolejno od prawej wartości odpowiadają kropce, a, b , c , d, e , f , g
// dataArray[2] = 0xB6; //11111100 FC
dataArray[0] = 0x7F;
dataArray[1] = 0x0C;
dataArray[2] = 0xB6;
dataArray[3] = 0x9E;
dataArray[4] = 0xCC;
dataArray[5] = 0xDA;
dataArray[6] = 0xFA;
dataArray[7] = 0x0E;
dataArray[8] = 0xFE;
dataArray[9] = 0xDE;
animacja[0] = 0x80; //g
animacja[1] = 0x20; //e
animacja[2] = 0x10; //d
animacja[3] = 0x08; //c
animacja[4] = 0x80; //g
animacja[5] = 0x40; //f
animacja[6] = 0x02; //a
animacja[7] = 0x04; //b
dummy[0] = 0x00;
randomSeed(analogRead(0));
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
anim();
} else {
idle();
digitalWrite(statusPin, LOW);
}
}
void idle(){
data = dummy[0];
digitalWrite(latchPin1, 0);
shiftOut(dataPin1, clockPin1, data);
digitalWrite(latchPin1, 1);
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, data);
digitalWrite(latchPin2, 1);
digitalWrite(latchPin3, 0);
shiftOut(dataPin3, clockPin3, data);
digitalWrite(latchPin3, 1);
}
// Funkcja odpowiedzialna za animację
void anim(){
for (int p =0; p < 3; p++){
for (int i = 0; i < 8; i++) {
data = animacja[i];
digitalWrite(latchPin1, 0);
shiftOut(dataPin1, clockPin1, data);
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, data);
digitalWrite(latchPin3, 0);
shiftOut(dataPin3, clockPin3, data);
digitalWrite(latchPin1, 1);
digitalWrite(latchPin2, 1);
digitalWrite(latchPin3, 1);
delay(100);
}
}
losLiczb(latchPin1, dataPin1, clockPin1, data1, liczba1);
Serial.println("Wylosowana liczba to");
Serial.println(liczba1);
for (int p =0; p < 2; p++){
for (int i = 0; i < 8; i++) {
data = animacja[i];
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, data);
digitalWrite(latchPin3, 0);
shiftOut(dataPin3, clockPin3, data);
digitalWrite(latchPin2, 1);
digitalWrite(latchPin3, 1);
delay(100);
}
}
losLiczb(latchPin2, dataPin2, clockPin2, data2, liczba2);
Serial.println("Wylosowana liczba to");
Serial.println(liczba2);
for (int p =0; p < 2; p++){
for (int i = 0; i < 8; i++) {
data = animacja[i];
digitalWrite(latchPin3, 0);
shiftOut(dataPin3, clockPin3, data);
digitalWrite(latchPin3, 1);
delay(100);
}
}
losLiczb(latchPin3, dataPin3, clockPin3, data3, liczba3);
Serial.println("Wylosowana liczba to");
Serial.println(liczba3);
if (data1 == data2){
digitalWrite(statusPin, HIGH);
} else {
digitalWrite(statusPin, LOW);
}
delay(5000);
}
//Funckja odpowiedzialna za wylosowanie liczby
int losLiczb(int myLatchPin, int myDataPin, int myClockPin, int myData, int myLiczba) {
//load the light sequence you want from array
int j = random(0, 9);
myLiczba = j;
myData = dataArray[j];
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(myLatchPin, 0);
//move 'em out
shiftOut(myDataPin, myClockPin, myData);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(myLatchPin, 1);
Serial.println("Wylosowana liczba to");
Serial.println(myLiczba);
}
// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut�
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}
Pass the values you want the function to output by reference or by pointer.
void Wrong(int a) {
a = 10;
}
void Right(int& a) {
a = 20;
}
void Right(int* pA) {
*pA = 30;
}
int a = 0;
Wrong(a); printf("%d ", a);
Right(a); printf("%d ", a);
Right(&a); printf("%d\n", a);
output will be 0 20 30