buttons/actions question

Hi

My name is Alf, from Chile.

Recently I start on Arduino world, and its fascinating. I have a little skills on BASIC and asm code for 6800, but I know nothing of C.

My question/problem: I want to control a some relays with buttons.

In BASIC is something like this...

1 BUTTON1=1 THEN GOSUB 10 (read pinx)
2 BUTTON2=1 THEN GOSUB 20 (read pinx)
3 BUTTON3=1 THEN GOSUB 30 (read pinx)
4 GOTO 1
10 RELAY1=1:RETURN (write pinX)
20 RELAY2=1":RETURN (write pinX)
30 RELAY3=1":RETURN (write pinX)

... the part I don't know is the use of gosub/return or goto.

Somebody help me?

have a look at the Arduino Tutorials and using buttons
Also a C tutorial would be useful.

This is my sketch for one button/ one relay:

const int buttonPin = 2;   
const int relPin =  13;     
int buttonState = 0;        

void setup() {
 
  pinMode(relPin, OUTPUT);
   pinMode(buttonPin, INPUT);
}

void loop() {

  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
        digitalWrite(relPin, HIGH);
  } else {
    
    digitalWrite(relPin, LOW);
  }
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Your code looks good, does it compile?
Have you tried it in an Arduino?

Tom.. :slight_smile:

the code looks OK - did you try building and loading it into an Arduino?
did it work?
remember to use code tags when including code

You may find something useful in Planning and Implementing a Program

...R

I have a little skills on BASIC and asm code for 6800, but I know nothing of C.

Well then, you need to do a C tutorial. As you already know how to program, picking up another language is reasonably easy.

Google "C++ tutorial". Run through the lessons - the first half, anyway. The second half will be stuff about classes that you don't really need to know yet.

Then google "how arduino programming differs from regular C programming", or something like that. You will learn that there isn't really a cout, not to overwrite main(), and not to use C++ Strings.

Then have a look at the examples in the file menu of the arduino IDE.

With all my respect, I dont want to learn C from zero, I´m just asking for help for a very simple thing. I'm administrator of a website and forum related to video games, in spanish, and when somebody ask for help, we give the responce, as soon as posible, no sending the guy to looking for the answer in another place, or "resolve the thing yourself". Thats why proudly call ourself "comunity".
Now, I enter to this forum looking for that little help, a kick-start. I never say "do me the entire sketch".
Thankyou very much, for nothing.

alf_onso:
With all my respect, I dont want to learn C from zero, I´m just asking for help for a very simple thing. I'm administrator of a website and forum related to video games, in spanish, and when somebody ask for help, we give the responce, as soon as posible, no sending the guy to looking for the answer in another place, or "resolve the thing yourself". Thats why proudly call ourself "comunity".
Now, I enter to this forum looking for that little help, a kick-start. I never say "do me the entire sketch".
Thankyou very much, for nothing.

It sounds like to want to learn how C++ is integrated into the Arduino environment.
YouTube has some very good tutorials for various aspects of C++ with Arduino.
Sparkfun is a good spot on YouTube.
If you use the YouTube search bar. Arduino Tutorials
you should be presented with a good list.
Hope this helps.. Tom.. :slight_smile:

We also call ourselves a community. When someone here asks for help, we assist their learning how to solve their own particular problem, instead of just giving them the answer. Think of the parable of teaching a man to fish, rather than giving him a ready made Filet O’ Fish sandwich*.

*But I am hungry!

What a difference, in my sites we are hungry to teach and help people. Sad.

So GOSUBs are functions in C/C++. E.g. digitalWrite and digitalRead are functions.

A function can return a value and can take parameters

/*
  function that does not take parameters and does not return anything
*/
void someFunc()
{
  ...
  ...
}

/*
  function that does not take parameters and returns an int
*/
int someFunc()
{
  int someValue;
  ...
  ...
  return someValue;
}

/*
  function that takes two parameters and and returns a bool
*/
bool someFunc(int a, int b)
{
  bool result = false;
  ...
  ...
  return result;
}

When functions reach the end (indicated by }), they automatically return, so there is no need for a return statement.

However, you need a return statement if the function is specified to return something (2nd and 3rd example); the compiler will generate a warning in the line of "reaching end of non-void function" if you don't and you might end up with mysterious issues at run time.

There is no need for GOTO; in your BASIC example it's needed to prevent the code from continuing with the first GOSUB.

alf_onso:
What a difference, in my sites we are hungry to teach and help people. Sad.

So are we; most of us however take a slow approach where (initially) teaching is far more important than the actual solution.

alf_onso:
With all my respect, I dont want to learn C from zero,

That's exactly why I gave you the link in Reply #5. Have you studied it?

However it is going to be difficult to write an Arduino program unless you know the rudiments of C/C++.

I wonder if the nature of the "product" you support on your website is different - are you helping people with questions about how to play games OR questions about how to write game programs using a low-level programming language?

...R

Let us convert your BASIC Program into a Flow Chart to visualize the course of actions to be taken when some excitation are applied from outside by pressing the buttons. After that we will see the ways that we follow to grasp the meanings of the Flow Chart using a sketch in the Arduino IDE using Arduino macros and C Programming Language Elements (the constructs).

A: hardware Block Diagram for the System

Figure-1: Block diagram showing a tentative connections among Buttons, Relays, and PC

B: Flow Chart for the Control program of Fig-1

Figure-2: Flow Chart for the user Control Program

Program Codes

void setup()
{
  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);  //DPin-2 is input line with internal pull-up connected
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);

  pinMode(8, OUTPUT);     //direction of DPin-8 is outgoing
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  digitalWrite(8, LOW);    //DPin-8 is at Logic Low
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
}

void loop()
{
  if (digitalRead(2) != HIGH)  //checking if Button1 has been closed
  {
    relay1Control();    //equivalent to GOSUB10
  }

  if (digitalRead(3) != HIGH) //checking if Button2 has been closed
  {
    relay2Control();    //equivalent to GOSUB20
  }

  if (digitalRead(4) != HIGH) //checking if button3 has been closed
  {
    relay3Control();    //equivalent to GOSUB30
  }
}

void relay1Control()
{
  //activate Relay1 (RL1)
  digitalWrite(8, HIGH);

  //retuen to Main Line Program (MLP)
}

void relay2Control()
{
  //activate Relay1 (RL2)
  digitalWrite(9, HIGH);

  //retuen to Main Line Program (MLP)
}

void relay3Control()
{
  //activate Relay1 (RL3)
  digitalWrite(10, HIGH);

  //retuen to Main Line Program (MLP)
}

You know what is sad is that so many here are trying to help you, yet you refuse to try to learn, only criticising the attempts because you feel that you cannot learn.