Arduino fuction outside loop [closed] - arduino

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 16 days ago.
Improve this question
im trying to use a func inside Loop in Arduino. It wont work;
Params are numeroPin, sonido and delay. I tried everything but it seems like im not able to fix this
void setup() {
for(int i = 2 ; i<=9; i++){
pinMode(i, OUTPUT);
}
}
int C4 = 262; // LED 4
int D4 = 294; // LED 5
int E4 = 330; // LED 6
int F4 = 349; // LED 7
int G4 = 392; // LED 8
int C5 = 523; // LED 9
void sonar(numeroPin, sonido, delay) {
tone(2, sonido);
digitalWrite(numeroPin, HIGH);
delay(delay);
digitalWrite(numeroPin, LOW);
}
void loop() {
sonar(4, C4, 1000);
sonar(5, D4, 1000);
sonar(6, F4, 167);
sonar(7, E4, 167);
sonar(5, D4, 167);
sonar(8, C5, 1000);
sonar(9, G4, 500);
}
I tried it and it fails to compile.

declare parameters type in your function :
void sonar(int numeroPin, int sonido, int mydelay)
and avoid 'delay' as variable name as it is system reserved.
void setup() {
for (int i = 2 ; i <= 9; i++) {
pinMode(i, OUTPUT);
}
}
int C4 = 262; // LED 4
int D4 = 294; // LED 5
int E4 = 330; // LED 6
int F4 = 349; // LED 7
int G4 = 392; // LED 8
int C5 = 523; // LED 9
void sonar(int numeroPin, int sonido, int mydelay) {
tone(2, sonido);
digitalWrite(numeroPin, HIGH);
delay(mydelay);
digitalWrite(numeroPin, LOW);
}
void loop() {
sonar(4, C4, 1000);
sonar(5, D4, 1000);
sonar(6, F4, 167);
sonar(7, E4, 167);
sonar(5, D4, 167);
sonar(8, C5, 1000);
sonar(9, G4, 500);
}

Related

Why is TCNT1 not counting up on Atmega328?

I have the following code for the Arduino with Atmega328 and a common 16x2 LCD. The LCD is working, but it is always showing the starting value "333" of the Timer 1 counter TCNT1. Why? I have read the datasheet of the 328 over and over again, but I don't get it.
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int lcdContrastPin = 6, lcdBackligthPin = 10;
void setup()
{
// tutn on LCD backlight and contrast
pinMode(lcdContrastPin, OUTPUT);
pinMode(lcdBackligthPin, OUTPUT);
// fine-tuning contrast could be done by PWM on lcdContrastPin
digitalWrite(lcdContrastPin, LOW);
digitalWrite(lcdBackligthPin, HIGH);
lcd.begin(16, 2);
// configure Timer1
TCCR1A = 0; // no waveform generation
TCCR1B = 0x00000010; // frequency divider 8 (i.e. counting with 2 MHz)
TCCR1C = 0;
TIFR1 = 0x00100000; // clear Input Capture Flag
TCNT1 = 333;
}
void loop()
{
int currentTimerValue = TCNT1;
lcd.setCursor(0, 0);
lcd.print("TCNT1=");
lcd.print(currentTimerValue);
lcd.println(" ");
delay(50);
}
Stupid me! In a lapse of consciousness I took 0x00000010 as a binary number instead of as a hexadecimal which it is. As a result I set the all clock selection bits to 0 which means the timer stops.
After replacing 0x00000010 by 0b00000010 (the true binary number) everything works as expected now:
TCCR1B = 0b00000010; // frequency divider 8 (i.e. counting with 2 MHz)
TCCR1C = 0;
TIFR1 = 0b00100000; // clear Input Capture Flag

Arduino-4X4 matrix keypad with I/O shift register

I need help. I have done some research and my little understanding of keypad scanning is that the ShiftIn value of Input Column should return zero (0) when a keypad button is pressed. Mine is only returning 255 (or 11111111) in BIN. All I need is to track the zero value when a key is pressed and then scan the keys matrix to display the pressed key. I will appreciate any help. I have added my code and schematic.
]1
const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;
char keys[kbdRows][kbdCols] = {
{ '1','2','3','4' },
{ '5','6','7','8' },
{ '9','0','A','B' },
{ 'C','D','E','F' }
};
byte KeyIsDown() {
int row;
int col;
int rowBits;
int colBits;
rowBits = 0X10;
for (row = 0; row < kbdRows; row++) {
digitalWrite(ClockPin, LOW);
digitalWrite(LatchOut, LOW);
shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
digitalWrite(LatchOut, HIGH);
delay(5);
digitalWrite(ClockPin, HIGH);
digitalWrite(LatchIn, LOW);
delay(5);
digitalWrite(LatchIn, HIGH);
colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
for (col = 0; col < kbdCols; col++) {
if (colBits==0) {
// not sure what condition to put here
byte keypressed = keys[kbdRows][kbdCols]; here
// I know this is the right stuff to return here
}
return colBits;
colBits = colBits >> 1;
}
rowBits = rowBits << 1;
}
}
void setup() {
pinMode(ClockPin, OUTPUT);
pinMode(DataOut, OUTPUT);
pinMode(DataIn, INPUT_PULLUP);
pinMode(LatchOut, OUTPUT);
pinMode(LatchIn, OUTPUT);
digitalWrite(LatchOut, HIGH);
digitalWrite(LatchIn, HIGH);
Serial.begin(9600);
digitalWrite(led, HIGH);
}
void loop() {
byte retColBit = KeyIsDown();
Serial.print("ColBit: ");
Serial.println(retColBit,BIN);
delay(500);
PinState = digitalRead(DataOut);
Serial.print("DataOut: ");
Serial.println(PinState,BIN);
delay(500);
}

Syncing Processing to an Arduino that is Multiplexing

I am sending 2 bytes from Processing to Arduino. These bytes are then stored in two values on the Arduino side, and then sent to a function that will then display them on a 7 segment 4 digit display.
The numbers are displayed fine! Yes, until about 5 seconds. I recorded it in slow motion.
9561 runs for about 5 seconds,
then a flash of 6100 very shortly,
and then 6195 very shortly
then it returns to 9561 for about 5 seconds again.
This can be changed slightly by altering the time each number is on the display, and I try to keep it below 4.
Anyways then changing the speeds on both programs also alters it, but I can't seem to get it right. Do I have to sync the programs through altering the speeds or is there a way to sync the numbers? I read into serialEvent() but not sure how to incorporate it into my project.
Here's my code below for the PROCESSING
void setup()
{
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
seg[0] = 9;
seg[1] = 5;
seg[2] = 6;
seg[3] = 1;
int x = (seg[pos+1] & 0x000f)<<4|(seg[pos]) & 0x000f; //3210
int x2 = (seg[pos+3] & 0x000f)<<4|(seg[pos+2] & 0x000f);
myPort.write(x);
myPort.write(x2);
//0123
int disp1 = x;
int disp2 = (x & 0x00f0)>>4;
int disp3 = (x2 & 0x000f);
int disp4 = (x2 & 0x00f0)>>4;
//0123
print(disp1, '\n', disp2, '\n', disp3, '\n', disp4, '\n', '\n');
}
And ARDUINO
void setup() {
pinMode(GND1, OUTPUT);
pinMode(GND2, OUTPUT);
pinMode(GND3, OUTPUT);
pinMode(GND4, OUTPUT);
pinMode(aPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(cPin, OUTPUT);
pinMode(dPin, OUTPUT);
pinMode(ePin, OUTPUT);
pinMode(fPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(light, OUTPUT);
Serial.begin(9600);
}
void digitdisplay(byte x,byte x2)
{
//unsigned char tn = int (num/1000);
//if (tn>10)
//{
// tn = tn - 10;
//}
//
//unsigned char hn = int (num/100)%10;
//unsigned char tenn = int (num/10)%10;
//unsigned char on = int (num%10);
int disp[4];
disp[0] = ( x & 0x000f);
disp[1] = ( x & 0x00f0)>>4;
disp[2] = ( x2 & 0x000f);
disp[3] = ( x2 & 0x00f0)>>4;
numberselect(disp[0]);
digitselect(1);
delay(t);
numberselect(disp[1]);
digitselect(2);
delay(t);
numberselect(disp[2]);
digitselect(3);
delay(t);
numberselect(disp[3]);
digitselect(4);
delay(t);
}
void loop()
{
int x,x2;
if (Serial.available()>0)
{ // If data is available to read,
x=Serial.read();
x2=Serial.read();
}
//shiftlight(maxrange);
digitdisplay(x,x2);
}

Arduino for Christmas light control - invert code

I have an arduino uno connected to a 8 channel relay board. I want to use it with Vixen 3. When I upload the code all the relays turn on. So, when i send a signal in Vixen the relay turns off. I need a way to invert this so when I send the signal from Vixen it turns on the relay. The coding side isn't really my strong point, so please go easy on me.
With love xx
int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];
void setup()
{
Serial.begin(9600);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
}
void loop()
{
if (Serial.available() >= 8) {
for (int i=0; i<=8; i++)
{
incomingByte[i] = Serial.read();
}
analogWrite(C1, incomingByte[0]);
analogWrite(C2, incomingByte[1]);
analogWrite(C3, incomingByte[2]);
analogWrite(C4, incomingByte[3]);
analogWrite(C5, incomingByte[4]);
analogWrite(C6, incomingByte[5]);
analogWrite(C7, incomingByte[6]);
analogWrite(C8, incomingByte[7]);
}
}
Try this code:
int C1 = 2;
int C2 = 3;
int C3 = 4;
int C4 = 5;
int C5 = 6;
int C6 = 7;
int C7 = 8;
int C8 = 9;
int i = 0;
int incomingByte[8];
void setup()
{
Serial.begin(9600);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
digitalWrite(C1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C3, LOW);
digitalWrite(C4, LOW);
digitalWrite(C5, LOW);
digitalWrite(C6, LOW);
digitalWrite(C7, LOW);
digitalWrite(C8, LOW);
}
void loop()
{
if (Serial.available() >= 8) {
for (int i = 0; i <= 8; i++)
{
incomingByte[i] = Serial.read();
}
analogWrite(C1, incomingByte[0]);
analogWrite(C2, incomingByte[1]);
analogWrite(C3, incomingByte[2]);
analogWrite(C4, incomingByte[3]);
analogWrite(C5, incomingByte[4]);
analogWrite(C6, incomingByte[5]);
analogWrite(C7, incomingByte[6]);
analogWrite(C8, incomingByte[7]);
}
}

Why is this not controlling my shift registers?

this has been cooking my brain for hours. I just don't get what I'm doing wrong. The code seems to be wrong when entering the "if (seperate == ..)" loop. So I want to read from serial, apply the value to the first register, then read and apply to the second register, then read and apply to the third register and finally "reset" the counter(and start over).
#define data_r 2 //data input red register
#define data_g 3 //data input green register
#define data_b 4 //data input blue register
#define clock_r 6 //clock input red register
#define clock_g 7 //clock input green register
#define clock_b 8 //clock input blue register
#define clock 9 //clock pin for all three registers
#define reset 5
int seperate;
int val;
void setup() {
// put your setup code here, to run once:
pinMode(clock, OUTPUT);
pinMode(clock_r, OUTPUT);
pinMode(clock_g, OUTPUT);
pinMode(clock_b, OUTPUT);
pinMode(data_r , OUTPUT);
pinMode(data_g , OUTPUT);
pinMode(data_b , OUTPUT);
pinMode(reset, OUTPUT);
digitalWrite(reset, LOW);
delay(50);
digitalWrite(reset, HIGH);
Serial.begin(9600);
while (!Serial);
Serial.setTimeout(10);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
int seperate_mod = seperate % 3;
Serial.print("seperate_mod = ");
Serial.println(seperate_mod);
if (seperate_mod == 0) {
val = Serial.parseInt();
shiftOut(data_r, clock_r, MSBFIRST, val);
} else if (seperate_mod == 1) {
val = Serial.parseInt();
shiftOut(data_g, clock_g, MSBFIRST, val);
} else if (seperate_mod == 2) {
val = Serial.parseInt();
shiftOut(data_b, clock_b, MSBFIRST, val);
}
seperate += 1;
}
}
But even more strange is that THIS CODE does work, it's only using one register:
#define data 2
#define clock 6
#define reset 5
void setup()
{
pinMode(clock, OUTPUT); // make the clock pin an output
pinMode(data , OUTPUT); // make the data pin an output
pinMode(reset, OUTPUT);
randomSeed(analogRead(0));
digitalWrite(reset, LOW);
delay(1500);
digitalWrite(reset, HIGH);
Serial.begin(9600);
Serial.setTimeout(10);
while(!Serial);
}
void loop() {
if (Serial.available()) {
int val = Serial.parseInt();
if (val > 255) {
Serial.println("ERR: Value out of range");
} else {
shiftOut(data, clock, LSBFIRST, val);
}
}
}
Could someone tell me please what I am doing wrong?
To zmo:
5
5
5
5
5
>ENTERED A NUMBER<
0 ; seperate_mod = 0
1
4 ; END of conditional
5
5
5
5
5
5
5
5
5
5
5
5
5
5
>ENTERED A NUMBER<
0 ; seperate_mod = 1
2
4 ; END of conditional
5
5
5
5
5
5
>ENTERED A NUMBER<
0 ; seperate_mod = 2
3
4 ; END of conditional
5
5
5
5
5
5
5
Could someone tell me please what I am doing wrong?
your code looks mostly fine at first glance, so it's likely to be something else in the context of your code that's not going right.
Have you tried adding debugging printout statements?
Have you tried using a debugger (using a device such as the Atmel ICE)?
It's a sane thing to doubt yourself when what you wrote is not working the way you expect — assuming PEBCAK is often the first best answer. But then, running through your code, displaying values along the way will prove to you whether you're doing it right or wrong, and if it's wrong where is that.
Then, it feels to me like your code isn't actually running at all, either because Serial initialisation is blocking forever in setup() or because Serial.available() is never being true for some other reasons.
Can you try adding that other debug print statement:
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
int seperate_mod = seperate % 3;
Serial.print("0 ; seperate_mod = ");
Serial.println(seperate_mod);
if (seperate_mod == 0) {
Serial.println("1");
val = Serial.parseInt();
shiftOut(data_r, clock_r, MSBFIRST, val);
} else if (seperate_mod == 1) {
Serial.println("2");
val = Serial.parseInt();
shiftOut(data_g, clock_g, MSBFIRST, val);
} else if (seperate_mod == 2) {
Serial.println("3");
val = Serial.parseInt();
shiftOut(data_b, clock_b, MSBFIRST, val);
}
seperate += 1;
Serial.println("4 ; END of conditional");
}
Serial.println("5");
}
and update your question with the resulting output?

Resources