Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedYousryM committed Jan 22, 2024
1 parent 6683e01 commit 0f83df4
Show file tree
Hide file tree
Showing 7 changed files with 711 additions and 0 deletions.
139 changes: 139 additions & 0 deletions Examples/Chat_with_Arduino/Chat_with_Arduino.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include <NonBlockingSequence.h>

NonBlockingSequence chat;
NonBlockingSequence subsequence;
String TotalString;
String SubSeqString;
unsigned int char_no = 0;
unsigned int SubSeqchar_no = 0;
String UserName;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);
//delay(1500);
Add_sentence("Hi.");
chat.AddDelayInMillis(1000);
Add_sentence("I hope you have a good day.");
chat.AddDelayInMillis(1000);
Add_sentence("My name is chatichat.");
chat.AddDelayInMillis(1000);
Add_sentence("What is your name?");
chat.AddDelayInMillis(1000);
chat.AddNewStep( &NewLine );
chat.AddNewStep( &RecieveName );
chat.AddNewStep( &ifNameisRecieved );
chat.AddNewStep( &subSeq_Respond );
chat.AddDelayInMillis(1000);
Add_sentence("I can count from 1 to 10");
chat.AddDelayInMillis(1000);
Add_sentence("1 2 3 4 5 6 7 8 9 10");
chat.AddDelayInMillis(1000);
Add_sentence("Do you want to repeat this conversation? (y/n)");
chat.AddDelayInMillis(1000);
chat.AddNewStep( &Respond );
Add_sentence("Good bye.");
chat.AddDelayInMillis(1000);

Add_sentence_to_subsequence("Nice Name");
subsequence.AddDelayInMillis(1000);
}

void loop() {
// put your main code here, to run repeatedly:
chat.DoSequence();
}

void Add_sentence_to_subsequence(String str){
SubSeqString=SubSeqString+str;
for(unsigned int i = 0; i<str.length(); i++){
subsequence.AddNewStep( &SubSeqWriteChar );
subsequence.AddDelayInMillis(100);
}
subsequence.AddNewStep( &NewLine );
}

void Add_sentence(String str){
TotalString=TotalString+str;
for(unsigned int i = 0; i<str.length(); i++){
chat.AddNewStep( &WriteChar );
chat.AddDelayInMillis(100);
}
chat.AddNewStep( &NewLine );
}

bool WriteChar(){
Serial.print(TotalString.charAt(char_no));
char_no++;
return true;
}

bool SubSeqWriteChar(){
Serial.print(SubSeqString.charAt(SubSeqchar_no));
SubSeqchar_no++;
return true;
}

bool NewLine(){
Serial.println();
}

bool subSeq_Respond(){
subsequence.DoSequence();
return subsequence.Finish();
}

bool Respond(){
if (chat.first_time_exexuting_step==true){
while (Serial.available() > 0) {Serial.read();}
}

if (Serial.available()){
char ch=Serial.read();
if ((ch=='y')|(ch=='Y')){
chat.Restart();
subsequence.Restart();
char_no = 0;
SubSeqchar_no = 0;
UserName="";
return 0;
}else if((ch=='n')|(ch=='N')){
return 1;
}else{
return 0;
}
}else{
return 0;
}
}

bool ifNameisRecieved(){
if (UserName.length()<=3){chat.PassSteps(2);}
return 1;
}


unsigned long mm;
bool RecieveName(){

if (chat.first_time_exexuting_step==true){
mm=millis();
while (Serial.available() > 0) {Serial.read();}
return false;
}else{
if ((millis()-mm)>10000){
return true;
}else{
if (Serial.available()){
UserName=Serial.readString();
Serial.println(UserName);
return true;
}else{
return false;
}
}
}


}
70 changes: 70 additions & 0 deletions Examples/Class_Functions/Class_Functions.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <NonBlockingSequence.h>

/*
This code illustrate How to declare a new class that run specific sequence
*/

class Blinking_Led{

public:
Blinking_Led(){};
void init(byte pin,unsigned long pause){

// define the led pin
_pin=pin;
pinMode(_pin,OUTPUT);

// This line explain important fact for the Sequence
Sequence.AttachedObj(this);
// Sequence should use steps defined inside this class only

// Add steps
Sequence.AddNewStep(&led_on);
Sequence.AddDelayInMillis(pause);
Sequence.AddNewStep(&led_off);
Sequence.AddDelayInMillis(pause);
Sequence.Repeat();
// repeat the sequence infinite number of times

//Serial.println("pointer: "+(String)((unsigned long)this)+" Pin: "+(String)((int)_pin));
}

Blink(){ // use familiar name for led
Sequence.DoSequence();
}
private:
// Declare Sequence
ClassNonBlockingSequence<Blinking_Led> Sequence;

// Define step-functions
bool led_on(){
digitalWrite(_pin,1);
return true;
}
bool led_off(){
digitalWrite(_pin,0);
return true;
}

// led pin
byte _pin;
};

// Declare new objects
Blinking_Led Led1;
Blinking_Led Led2;

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

// ( pin , blinking time )
Led1.init(13,200);
Led2.init(12,1500);

}

void loop() {
// put your main code here, to run repeatedly:
Led1.Blink();
Led2.Blink();
}
55 changes: 55 additions & 0 deletions Examples/Two_Blinking_led/Two_Blinking_led.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <NonBlockingSequence.h>

// Pin used to wire the leds
#define led1 13
#define led2 12

// two new sequences are defined
NonBlockingSequence Sequence1;
NonBlockingSequence Sequence2;

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

// Define steps for Sequence1
Sequence1.AddNewStep(&led1_on);
Sequence1.AddDelayInMillis(400);
Sequence1.AddNewStep(&led1_off);
Sequence1.AddDelayInMillis(400);
Sequence1.Repeat();

// Difinition for pin usage
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);

// Define steps for Sequence2
Sequence2.AddNewStep(&led2_on);
Sequence2.AddDelayInMillis(1500);
Sequence2.AddNewStep(&led2_off);
Sequence2.AddDelayInMillis(1500);
Sequence2.Repeat();

}

void loop() {
// put your main code here, to run repeatedly:
Sequence1.DoSequence();
Sequence2.DoSequence();
}

bool led1_on(){
digitalWrite(led1,1);
return true;
}
bool led1_off(){
digitalWrite(led1,LOW);
return true;
}
bool led2_on(){
digitalWrite(led2,1);
return true;
}
bool led2_off(){
digitalWrite(led2,LOW);
return true;
}
9 changes: 9 additions & 0 deletions MyLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "MyLinkedList.h"







//template class MyLinkedList<variable>;
70 changes: 70 additions & 0 deletions MyLinkedList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once
#ifndef MYLINKEDLIST_H
#define MYLINKEDLIST_H

#include <Arduino.h>


template <class T>
struct Element{
T data;
Element<T>* next=0;
int id_;
};



template <class T>
class MyLinkedList{
public:

int size_=0;
virtual void add_element(T data);
bool next();
void from_begining();
T get_element();

private:

Element<T>* first;
Element<T>* last;
Element<T>* current_element;
};

template <class T>
void MyLinkedList<T>::add_element(T data){
Element<T>* e = new Element<T>();;
e->data=data;
e->id_=size_;
if (size_==0){
first=e;
current_element=e;
}
else{
last->next=e;
}
last=e;
size_++;
}


template <class T>
bool MyLinkedList<T>::next(){
if (current_element->next != 0){
current_element=current_element->next;
return true;
}else{ return false; }

}

template <class T>
void MyLinkedList<T>::from_begining(){
current_element=first;
}

template <class T>
T MyLinkedList<T>::get_element(){
return current_element->data;
}

#endif

0 comments on commit 0f83df4

Please sign in to comment.